karosLi / KKJSBridge

一站式解决 WKWebView 支持离线包,Ajax/Fetch 请求,表单请求和 Cookie 同步的问题 (基于 Ajax Hook,Fetch Hook 和 Cookie Hook)
MIT License
693 stars 120 forks source link

feature: bridge 支持同步的 JS-Native 交互 #31

Closed wjiuxing closed 4 years ago

wjiuxing commented 4 years ago

通过原生的 window.webkit.messageHandlerspostMessage 的方式是异步的,有时候 JS 端也需要同步交互。

在此次 PR 中借助 webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:completionHandler: 的特性,实现了同步交互。

用法如下:

function syncCallFunction() {
    const parameters = {}; // or {'some': 'some value or empty is Okay'}
    const result = window.KKJSBridge.syncCall('module', 'method', parameters);
    alert('value: ' + result.value);
}
karosLi commented 4 years ago

你是什么场景下,需要同步的?

wjiuxing commented 4 years ago

H5 那边需要等到结果才执行后面的代码,无法推动他们改成异步回调的方式。改变不了别人,只能 改变自己了。。。

发自Jiuxing Wang's iPhone

在 2020年7月23日,下午6:41,Karosli notifications@github.com 写道:



你是什么场景下,需要同步的?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/karosLi/KKJSBridge/pull/31#issuecomment-662937132, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AA73W2F6IMU7ATNES3S5SA3R5AHXFANCNFSM4O7VQD4Q.

karosLi commented 4 years ago

那让H5自己封装成 promise,或者你这边注入js,提供promise的方法

wjiuxing commented 4 years ago

好的,我去请 h5 搞一下,谢谢了!