Understanding WKWebView NSCoding Support in Xcode 9

28 February 2025 Stephan Petzl Leave a comment Xcode

Developers working with Xcode 9 may encounter issues when integrating WKWebView within the Interface Builder. This article aims to clarify the behavior and provide solutions for those developing iOS applications targeting versions below iOS 11.

Why the Issue Occurs

The root of the problem lies in a bug present in the -[WKWebView initWithCoder:] method, which was only resolved in iOS 11. This bug caused runtime crashes when configuring a WKWebView in Interface Builder. As a result, Xcode 9 generates a build error to prevent developers from deploying potentially unstable applications.

Here are the most effective solutions to bypass this issue:

  • Programmatic Integration: The most reliable solution is to add the WKWebView programmatically. This approach bypasses the Interface Builder and allows compatibility with iOS versions below iOS 11. Here’s a basic implementation:
    import UIKit
    import WebKit
    
    class ViewController: UIViewController, WKUIDelegate {
        var webView: WKWebView!
    
        override func 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)
        }
    }
    
  • Adjust Build Settings: If your target iOS version is 11 or later, adjust your Interface Builder settings to reflect this. Navigate to your storyboard, select a scene, and in the ‘File Inspector’, set ‘Builds for’ to ‘iOS 11.0 and Later’.

Additional Considerations

While integrating WKWebView programmatically is a straightforward solution, it’s crucial to ensure that your application’s design and functionality requirements are met. Consider creating a container view in Interface Builder and adding the WKWebView as a subview programmatically to maintain design consistency.

Enhancing Your Testing Workflow with Repeato

When dealing with complex configurations like the WKWebView, testing becomes crucial. Tools like Repeato can significantly streamline the testing process. As a no-code test automation tool, Repeato allows you to create, run, and maintain automated tests efficiently. Its use of computer vision and AI makes it particularly adept at handling UI elements like WKWebView, ensuring that your applications function correctly across various scenarios.

Repeato’s ability to run command line scripts or JavaScript code provides flexibility in automating complex tasks, while its support for data-driven and keyword-driven testing enhances your testing strategy. This makes Repeato a practical alternative to other test automation tools, offering a seamless integration into your development workflow. For more details, explore our documentation.

Like this article? there’s more where that came from!