简体   繁体   中英

Unable to add WKWebView to Xcode Project

I have a ViewController that looks something like:

import UIKit
import WebKit

class ViewController: UIViewController {
    @IBOutlet var webView: WKWebView!
}

In my storyboard I have a WKWebView as a subview of my main view. I've linked up the IBOutlet but when I run the application I receive:

EXC_BAD_ACCESS (code=1, address=0x431)

在此处输入图片说明 在此处输入图片说明

What am I doing wrong here and how do I fix it?

WKWebView is not supported on Storyboards. You have to create it by code. The web view element you are using in your Storyboard is a UIWebView.

Use this simple and direct code to create and load your WKWebView:

        let webView = WKWebView(frame:NSMakeRect(0, 0,1000, 1000))
        webView.load(NSURLRequest(url:NSURL(string:"https://YourWebURLToLoad") as! URL) as URLRequest)
        webView.navigationDelegate = self;
        self.window.contentView?.addSubview(webView)

Two things to check: Did you import the framework in your target?

在此输入图像描述

If you have and the error persists, check the web view's connection inspector and see if it references any other outlets from the storyboard. I've been known to delete a previous connection and recreate it, so it no longer exists in code, but IB holds on to them. An error can be thrown if said prior connection no longer exists.

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM