iosswiftwkwebviewwebassemblyjavascriptcore

How to load WebAssembly in iOS app via WKWebVIew or JSC


I'm trying to load and execute a WebAssembly .wasm from within a Swift-based, iOS app. I first attempted to use the JavaScriptCore Framework but the WebAssembly.* module wasn't available in the Context when I tried to evaluate a trivial script. I was able to confirm the WebAssembly isn't defined via the Safari Debugger Console.

I then attempted to use WKWebView because I'm led to believe that the lack of WebAssembly is due to JSC not supporting JIT, which WKWebView should. I got the same result.

Here's a trivial app running on an iPhone X 12.4 emulator, Xcode 10.3 and the WebKit Framework manually added to the project. Make sure to open Safari and Select "Developer>Simulator" for WKWebView debugger.

import UIKit
import WebKit

class ViewController: UIViewController {
    @IBOutlet weak var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    @IBAction func onClick(_ sender: UIButton) {
        let testWasm = """
            if(typeof WebAssembly !== 'undefined') {
                console.log("Hello, Wasm.");
            } else {
                console.log("No Wasm for you!");
            }
        """

        webView.evaluateJavaScript(testWasm)
    }
}

Does iOS actually have a way to load WebAssembly into a Swift-based app?


Solution

  • I wanted to run some benchmarks with JSC to compare it with Wasm3.
    Basically I run into the same situation and wasn't able to fix so far.

    Wasm3 runs perfectly fine on iOS, and may suite your needs at the moment.

    Currently you can't run WebAssembly with JSC on the simulator according to this comment.