Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKWebView in Interface Builder

It seems that the IB object templates in XCode 6 beta are still creating old-style objects (UIWebView for iOS and WebView for OSX). Hopefully Apple will update them for the modern WebKit, but until then, what is the best way to create WKWebViews in Interface Builder? Should I create a basic view (UIView or NSView) and assign its type to WKWebView? Most of the examples I find online add it to a container view programmatically; is that better for some reason?

like image 653
Adam Fox Avatar asked Jun 11 '14 16:06

Adam Fox


People also ask

What is a WKWebView?

Overview. A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.

What is the difference between UIWebView and WKWebView?

Difference Between UIWebview and WKWebViewUIWebview is a part of UIKit, so it is available to your apps as standard. You don't need to import anything, it will we there by default. But WKWebView is run in a separate process to your app,. You need to import Webkit to use WKWebView in your app.

Is WKWebView the same as Safari?

WKWebView - This view allows developers to embed web content in your app. You can think of WKWebView as a stripped-down version of Safari. It is responsible to load a URL request and display the web content. WKWebView has the benefit of the Nitro JavaScript engine and offers more features.

Is WKWebView deprecated?

Since then, we've recommended that you adopt WKWebView instead of UIWebView and WebView — both of which were formally deprecated. New apps containing these frameworks are no longer accepted by the App Store.


2 Answers

You are correct - it doesn't seem to work. If you look in the headers, you'll see:

- (instancetype)initWithCoder:(NSCoder *)coder NS_UNAVAILABLE; 

which implies that you can't instantiate one from a nib.

You'll have to do it by hand in viewDidLoad or loadView.

like image 82
EricS Avatar answered Oct 07 '22 21:10

EricS


As pointed out by some, as of Xcode 6.4, WKWebView is still not available on Interface Builder. However, it is very easy to add them via code.

I'm just using this in my ViewController. Skipping Interface builder

import UIKit import WebKit  class ViewController: UIViewController {      private var webView: WKWebView?      override func loadView() {         webView = WKWebView()          //If you want to implement the delegate         //webView?.navigationDelegate = self          view = webView     }      override func viewDidLoad() {         super.viewDidLoad()          if let url = URL(string: "https://google.com") {             let req = URLRequest(url: url)             webView?.load(req)         }     } } 
like image 35
Johan Avatar answered Oct 07 '22 19:10

Johan