JavaScript synchronous native communication to WKWebView

前端 未结 4 539
我在风中等你
我在风中等你 2020-11-29 23:21

Is synchronous communication between JavaScript and Swift/Obj-C native code possible using the WKWebView?

These are the approaches I have tried and have failed.

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 23:44

    I found a hack for doing synchronous communication but haven't tried it yet: https://stackoverflow.com/a/49474323/2870783

    Edit: Basically you can use the the JS prompt() to carry your payload from the js side to the native side. In the native WKWebView will have to intercept the prompt call and decide if it is a normal call or if it is a jsbridge call. Then you can return your result as a callback to the prompt call. Because the prompt call is implemented in such a way that it waits for user input your javascript-native communication will be synchronous. The downside is that you can only communicate trough strings.

提交回复
热议问题