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?
You can use WebAssembly in Safari, Chrome, Firefox, Edge and in mobile browsers like iOS Safari, Firefox for Android, and Chrome.
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.
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.
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.
It appears that JavaScriptCore now has a WebAssembly interpreter, as of Feb 2020.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With