Xcode 9 GM support - WKWebView NSCoding was broken in previous versions

Does anyone know how to fix this error with Xcode 9 GM? I am working on an application created using Xcode 8.3, the deployment target is for iOS 9.3, and I have never had this problem before. I do not find any information here or on the Apple forums :(

Change: this error occurred when I put WKWebView in the interface constructor, and not if I use it programmatically.

See picture WKWebView Error

Edit 2: Well, this is finally not a mistake, see Quinn's answer below for more information on this behavior. Thanks to him for the explanation.

+130
ios xcode wkwebview nscoding
Sep 14 '17 at 14:20
source share
9 answers

An error is the correct behavior, not an error in Xcode 9. Although WKWebView was introduced in iOS 8, there was an error in -[WKWebView initWithCoder:] that was fixed only in iOS 11, which always crashed at runtime and therefore prevented configuration in the interface. Builder

https://bugs.webkit.org/show_bug.cgi?id=137160

Instead of allowing developers to create something in IB that will be broken at runtime, this is a build error. This is an inconvenient limitation, since iOS 11 was released recently, but there is no other good option.

A workaround for old deployment goals is to continue adding WKWebView to the code, as @ fahad-ashraf already described in its answer:

https://developer.apple.com/documentation/webkit/wkwebview

+133
Oct 09 '17 at 14:57
source share
โ€” -

This seems to be a bug in Xcode 9 and was also present in beta versions. You will receive a build error only if you create a WKWebView through a storyboard. If you gradually create a WKWebView in the appropriate ViewController class file, you can use versions for iOS below iOS 11. Here is the approach suggested on the Apple website to achieve this:

 import UIKit import WebKit class 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) }} 

After that, you can implement the functionality of WKWebView, as usual.

Source: https://developer.apple.com/documentation/webkit/wkwebview

+80
Sep 21 '17 at 14:35
source share

If you want to implement a custom UIViewController with other components in addition, you can create a โ€œcontainerโ€ through a storyboard called, for example, webViewContainer :

 import UIKit import WebKit class 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: 0.0, 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) } 
+50
Oct 18 '17 at 8:35
source share

If you switched from the old target to iOS 11.0 and still get this error, use the solution below.

  1. Go to the storyboard (Main.storyboard), click on any scene.
  2. Select "File Inspector", which is the right Xcode properties window
  3. Change Builds value for on iOS 11.0 and later '
  4. . Compile and build

enter image description here

+33
Sep 24 '18 at 6:33
source share

I ran into the same problem, but it can be solved if we add the WKWebView program code.

  • Make any design you want to make in the storyboard, but leave room for WKWebView , drag the view in this area and name it as webViewContainer and declare these two properties.

     @property (weak, nonatomic) IBOutlet UIView *webViewContainer; @property(nonatomic, strong)WKWebView *webView; 
  • Now create and add webView as follows:

     -(instancetype)initWithCoder:(NSCoder *)aDecoder { self.webView = [self createWebView]; self = [super initWithCoder:aDecoder]; return self; } 

    The createWebView function is declared as

     -(WKWebView *)createWebView { WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init]; return [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration]; } 
  • Now add this newly created webView to your container view, for example:

     -(void)addWebView:(UIView *)view { [view addSubview:self.webView]; [self.webView setTranslatesAutoresizingMaskIntoConstraints:false]; self.webView.frame = view.frame; } 
  • Finally, just load your url like this,

     -(void)webViewLoadUrl:(NSString *)stringUrl { NSURL *url = [NSURL URLWithString:stringUrl]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [self.webView loadRequest:request]; } 

Thanks for reading this.

+12
Nov 02 '17 at 7:34 on
source share

This method only works for some website. I tried http://www.google.com and it does not load.

+1
Oct 14 '17 at 9:34 on
source share

WebKit was introduced in iOS 8 , but was released with an error that caused a crash at runtime. If you are using Xcode 9/10 , your project configuration support is less than iOS 11 and if you are trying to add WKWebView using the interface designer. Xcode immediately shows an error at compile time.

WKWebView before iOS 11.0 (NSCoding support was interrupted in the previous version)

Although WKWebView was introduced in iOS 8 , there was a bug in [WKWebView initWithCoder:] that was fixed only in iOS 11 .

enter image description here

Solution - you should add WKWebView via code (only if you support below iOS 11). It actually sounds weird.

Another solution is to change the Interface Builder document assemblies for this option on iOS 11 and later (if you upgrade from an older target to iOS 11 and still get the same error).

0
Sep 18 '19 at 2:54
source share

I also had this problem, but when I set the development goal to 11.0, the project may work, but the error still exists.

-four
Sep 15 '17 at 9:23
source share

UIWebView is deprecated in iOS11, and WKWebView only works in iOS11 - this sounds like an error in Xcode GM.

-5
Sep 15 '17 at 14:20
source share



All Articles