4 ms·
Wait, websites can call native Swift methods from JS?
by throwaway290 4y ago
Wait, websites can call native Swift methods from JS?
- Linkd 4y agoCertainly. See https://developer.apple.com/documentation/webkit/wkscriptmessagehandler https://developer.apple.com/documentation/webkit/wkscriptmes...
- _rend 4y agoOnly intentionally, via setup from a hosting app. If an app uses a WKWebView to display web content, it can use WKUserContentController[1] to inject scripts and additional content into the page dynamically, and can inject functions into JS[2] which will trigger native callback handlers when called. If your app uses the JavaScriptCore[3] framework to run JS in a VM in-process directly, you have even more options for interfacing between JS and native code. Note that this has to be explicitly hooked up by the app (i.e., none of this applies within, say, Safari). [1]: https://developer.apple.com/documentation/webkit/wkusercontentcontroller https://developer.apple.com/documentation/webkit/wkuserconte... [2]: https://developer.apple.com/documentation/webkit/wkusercontentcontroller/1537172-add https://developer.apple.com/documentation/webkit/wkuserconte... [3]: https://developer.apple.com/documentation/javascriptcore https://developer.apple.com/documentation/javascriptcore
- Spivak 4y agoShort answer: yes With the appropriate libraries you can use JS to call Swift and Obj C code. Long answer: no All it really means is that the JS and Swift/Obj C can pass data between each other and the library is set up to parse that data and call the appropriate code. It's just an automatic RPC.