WKWebView in Interface Builder WKWebView in Interface Builder ios ios

WKWebView in Interface Builder


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.


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 UIKitimport WebKitclass 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)        }    }}


Info.plist

add in your Info.plist transport security setting

 <key>NSAppTransportSecurity</key> <dict>    <key>NSAllowsArbitraryLoads</key>    <true/> </dict>

Xcode 9.1+

Using interface builder

You can find WKWebView element in the Object library.

enter image description here

Add view programmatically with Swift 5

let webView = WKWebView(frame: .zero, configuration: WKWebViewConfiguration())view.addSubview(webView)webView.translatesAutoresizingMaskIntoConstraints = falsewebView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = truewebView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor).isActive = truewebView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = truewebView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor).isActive = true

Add view programmatically with Swift 5 (full sample)

import UIKitimport WebKitclass ViewController: UIViewController {    private weak var webView: WKWebView!    override func viewDidLoad() {        super.viewDidLoad()        initWebView()        webView.loadPage(address: "http://apple.com")    }    private func initWebView() {        let webView = WKWebView(frame: .zero, configuration: WKWebViewConfiguration())        view.addSubview(webView)        self.webView = webView        webView.translatesAutoresizingMaskIntoConstraints = false        webView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true        webView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor).isActive = true        webView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true        webView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor).isActive = true    }}extension WKWebView {    func loadPage(address url: URL) { load(URLRequest(url: url)) }    func loadPage(address urlString: String) {        guard let url = URL(string: urlString) else { return }        loadPage(address: url)    }}