作者:tingel34 | 来源:互联网 | 2023-08-13 14:15
根据@Alex pelletier的回答(对我有很大帮助),这是我的问题的解决方案。
在我的“ loadView()”函数中,这是我拥有的:
let cOntentController= WKUserContentController();
contentController.addScriptMessageHandler(
self,
name: "callbackHandler"
)
let cOnfig= WKWebViewConfiguration()
config.userCOntentController= contentController
webView = WKWebView(frame: CGRectZero, configuration: config)
webView.navigatiOnDelegate= self
view = webView
我处理发送到Swift的Javascript事件的函数:
func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage)
{
if(message.name == "callbackHandler") {
print("Launch my Native Camera")
}
}
…最后,当我的相机按钮(以HTML格式)单击时,我的Javascript(jQuery)代码:
$(document).ready(function() {
function callNativeApp () {
try {
webkit.messageHandlers.callbackHandler.postMessage("camera");
} catch(err) {
console.log('The native context does not exist yet');
}
}
$(".menu-camera-icon").click(function() {
callNativeApp();
});
});
我希望它会对其他人有所帮助:-)!