Webview crashes when instantiating shared workers in IOS 16.1

Some of our users are facing crashes related to the webview when opening our app on IOS 16.1 devices, IOS 16.0 is not affected.

Issue in the react-native-webview repo: https://github.com/react-native-webview/react-native-webview/issues/2718

I was able to put together a Swift WKWebView POC to reproduce the issue: https://github.com/mauri870/react-native-webview-ios-16.1-crash

It should work fine with 16.0 but crashes the native webview process in 16.1.

Post not yet marked as solved Up vote post of mauri870 Down vote post of mauri870
4.9k views
  • Same crash is happening in my coredova application for iOS 16 only. Added above script in CDVWebViewEngine, still crashing.

    NSString *scriptcode = [NSString stringwithFormate:@"deletewindow.SharedWorker;"]; WKUserScript *wkScript = [WKUserScript alloc] initWithSource:scriptCode injectionTime:WKUserScriptInjectionTimeAtDocumentStart for mainframe only:YES]; [userContentcontroller addUserScript]; CDVViewcontrollerer webview is getting nil hence causes crash. Looking forward for any help Thanks.

Add a Comment

Replies

Could you create a bug report for this issue (https://developer.apple.com/bug-reporting/) and post the bug number here?

Yes, the bug report number is FB11723920

Hello @mauri870. Thanks for reporting this issue regarding SharedWorker in WKWebView on iOS 16.1, and for the bug report.

While our engineers work on a long-term solution, the suggested workaround is to disable SharedWorkers for the web site you're loading.

You can either do this in your web source, if you control it, or in your native app, if you are loading arbitrary web sites.

As an example, the sample code in Code Listing 1 disables SharedWorkers when loading a page in WKWebView, by injecting a script using WKUserScript.

Code Listing 1.

func makeViewConfiguration() -> WKWebViewConfiguration
{
    let configuration = WKWebViewConfiguration()
    let dropSharedWorkersScript = WKUserScript(source: "delete window.SharedWorker;", injectionTime: WKUserScriptInjectionTime.atDocumentStart, forMainFrameOnly: false)
    configuration.userContentController.addUserScript(dropSharedWorkersScript)
    return configuration
}
var view = WKWebView(frame: CGRect(x: 0, y: 0, width: 400, height: 400), configuration: makeViewConfiguration())

Important: This code was minimally tested to ensure it builds, runs, injects the script, and disables SharedWorkers. It may require changes for your specific use.

You can read more about this issue on the WebKit GitHub page.

—Jason.

Thanks for the support Jason, looking forward to have this issue fixed.

I have the same problem but adding the code to remove SharedWorkers is working fine.

Had the same issue. The code fixed it. Thanks

Have same issue in My cordova application. Even after applying above script Still its crashing in iOS 16.

Added script like below in CDVWebEngine.


    WKUserScript *wkScript = [[WKUserScript alloc] initWithSource:scriptCode injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];

    [userContentController addUserScript:wkScript];

    WKWebViewConfiguration* configuration = [self createConfigurationFromSettings:settings];

    configuration.userContentController = userContentController;

    WKWebView* wkWebView = [[WKWebView alloc] initWithFrame:self.engineWebView.frame configuration:configuration];
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}
        if let tempWidgetVC =  storyboard.instantiateViewController(withIdentifier: "SampleController") as? SampleController {
            tempWidgetVC.view.frame = CGRect(x: 0, y: 135, width: self.view.frame.size.width, height: self.view.frame.size.height - 150)
             self.view.addSubview(tempWidgetVC.webView)
            }

webview is getting nil in iOS 16 hence crash is happening on self.view.addSubview(tempWidgetVC.webView)

Looking forward for any help suggestion

Thanks

  • Any suggestions ?

Add a Comment