Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 286
jonathanberi Avatar asked Aug 04 '19 17:08

jonathanberi


People also ask

Does WebAssembly run on iOS?

You can use WebAssembly in Safari, Chrome, Firefox, Edge and in mobile browsers like iOS Safari, Firefox for Android, and Chrome.

What is use of UIWebView or WKWebView?

A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.

How do I use WKWebView?

Here's how: Open the XIB or Storyboard you want to add the web view to in Interface Builder. Find the web view or WKWebView in the Object Library at the bottom-left of Interface Builder. Drag-and-drop a WKWebView object from the Object Library to your view controller's canvas, and adjust its size and position.


2 Answers

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.

like image 179
Volodymyr Shymanskyy Avatar answered Sep 22 '22 07:09

Volodymyr Shymanskyy


It appears that JavaScriptCore now has a WebAssembly interpreter, as of Feb 2020.

like image 21
Elijah Yap Avatar answered Sep 20 '22 07:09

Elijah Yap