Skip to content
Advertisement

is it possible to run swift on a webpage like js or swift to interact with JS

I’m not sure how viable this is (from a web performant POV) but I was wondering if Swift code can be run inside of a website?

For example adding to your HTML mark up like a script tag for JS?

Use case:

IOS has a low powermode that prevents some website features (such as autoplay video) – while you can use js event listener to detect suspend I have found this unreliable as a metric (as clicking off page causes suspend on video).

I was wondering if swift can be used to quickly detect if lowpower mode is being used and pass/create some flag for JS to detect and alter the design / functions accordingly.

I briefly read through the Swift docs but could use a swift developer weighing in on the possibilities of this workaround.

IDEALLY there would be a media query for low power mode like: @media and (low-power){} but this does not seem to be a current feature for CSS

Advertisement

Answer

Yes and no. Swift runs in the browser but it cannot access system APIs unless you run it inside a custom-made browser.

Running swift in a browser

It is possible to run Swift code in a browser. You can use SwiftWasm for that. Some practical information can be found in the SwiftWasm book

Swift ⇄ javascript interaction

It is also possible to interact with javascript. See: https://github.com/swiftwasm/JavaScriptKit

System API Access

It is not possible to access system APIs through a normal browser though. If you want access to those APIs, you should create a native app. Note that from within native apps you can also create web content. In such apps you do have access to system APIs through WKUserContentController‘s add(_:name:) method.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement