Xcode 9 GM - WKWebView NSCoding support was broken in previous versions
The error is correct behavior, and not a bug in Xcode 9. Although WKWebView was introduced in iOS 8, there was a bug in -[WKWebView initWithCoder:]
that was only fixed in iOS 11, which always crashed at runtime and thus prevented configuring one within Interface Builder.
https://bugs.webkit.org/show_bug.cgi?id=137160
Rather than allow developers to build something in IB that would be broken at runtime, it is a build error. It's an inconvenient limitation since iOS 11 was only recently released, but there's really no other good option.
The workaround for older deployment targets is to continue to add the WKWebView in code, as @fahad-ashraf already described in his answer:
This seems to be a bug in Xcode 9 and was also present in the betas. You will only get the build error if you are creating a WKWebView through the storyboard. If you progmatically create the WKWebView in the corresponding ViewController class file, you should be able to build on iOS versions below iOS 11. Here is the approach given on Apple's website for how to accomplish this:
import UIKitimport WebKitclass ViewController: UIViewController, WKUIDelegate { var webView: WKWebView! override func loadView() { super.loadView() let webConfiguration = WKWebViewConfiguration() webView = WKWebView(frame: .zero, configuration: webConfiguration) webView.uiDelegate = self view = webView } override func viewDidLoad() { super.viewDidLoad() let myURL = URL(string: "https://www.apple.com") let myRequest = URLRequest(url: myURL!) webView.load(myRequest) }}
You should then be able to implement WKWebView functionality as you normally would.
Source: https://developer.apple.com/documentation/webkit/wkwebview
If you want to realize a custom UIViewController
with other components in addition you can make a "container" through the storyboard called for example webViewContainer
:
import UIKitimport WebKitclass ViewController: UIViewController, WKUIDelegate { @IBOutlet weak var webViewContainer: UIView! var webView: WKWebView! override func viewDidLoad() { super.viewDidLoad() let webConfiguration = WKWebViewConfiguration() let customFrame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: self.webViewContainer.frame.size.width, height: self.webViewContainer.frame.size.height)) self.webView = WKWebView (frame: customFrame , configuration: webConfiguration) webView.translatesAutoresizingMaskIntoConstraints = false self.webViewContainer.addSubview(webView) webView.topAnchor.constraint(equalTo: webViewContainer.topAnchor).isActive = true webView.rightAnchor.constraint(equalTo: webViewContainer.rightAnchor).isActive = true webView.leftAnchor.constraint(equalTo: webViewContainer.leftAnchor).isActive = true webView.bottomAnchor.constraint(equalTo: webViewContainer.bottomAnchor).isActive = true webView.heightAnchor.constraint(equalTo: webViewContainer.heightAnchor).isActive = true webView.uiDelegate = self let myURL = URL(string: "https://www.apple.com") let myRequest = URLRequest(url: myURL!) webView.load(myRequest) }