Search code examples
javascriptiosuiwebviewwkwebviewprocessing.js

Why does WKWebView fails to run processingjs script?


I need to run some processing.js script in iOS's webview (html/js in local resources). First I tried with UIWebView, then I see documentation suggests to use WKWebView for better performance. So I wrote the following code:

    if NSClassFromString("WKWebView") != nil {
        let webConfiguration = WKWebViewConfiguration()
        wkWebView = WKWebView(frame: self.view.bounds, configuration:webConfiguration)
        wkWebView!.scrollView.isScrollEnabled=false
        wkWebView!.uiDelegate = self
        self.view.addSubview(wkWebView!)
        wkWebView!.load(request)
    } else {
        uiWebView = UIWebView(frame: self.view.bounds)
        uiWebView!.scrollView.isScrollEnabled=false
        self.view.addSubview(uiWebView!)
        uiWebView!.loadRequest(request)
    }
  • The processing animation only runs in UIWebView, but not in WKWebView; the page itself (elements other than the canvas) are rendered correctly with WK, but canvas is blank.
  • I have tried this on iOS 10.3, iOS 8.
  • I thought maybe I need to configure the WKWebView somehow. But didn't find such configuration options in documentation.

Anyone notice the same problem here?

And relevant to this question: how should I debug javascript within the webview in iOS (something like a console in desktop Chrome)?


Solution

  • I can at least answer the debugging aspect for sure (and give pointers about why it might fail in general).

    Debugging can be done in your (macOS) Safari. Activate the Developer menu (Settings - Advanced - "Show Develop menu in menu bar"). In said menu, once your app runs (in the Simulator or on a connected device) you can select your device/simulator and then connect to your app (or rather the webview).

    In general, the issue might be a file permissions or even ATS problem. If I got you correctly you're constructing your request from a local file. This might have an effect on how it's treated, but I admit I am not sure. WKWebView is stricter in that regard. You might also want to ensure that the WKPreferences object that the standard configuration uses satisfies your needs. It has several properties regarding Java and JS (JS is generally allowed by default, but it can't open windows, for example). Oh, and I assume you didn't deactivate JS in the Settings app of the device/Simulator?