使用 Swift 在 iOS WKWebview 中捕获 Javascript 事件

新手上路,请多包涵

我正在使用网络编程语言构建一个应用程序,并希望在用户单击 HTML 按钮时启动相机。因为我希望我的相机视图是自定义视图,所以我需要使用 Swift 来设计它。所以当用户点击这个 HTML 按钮时,我想在 Swift 中“捕捉”这个点击,这样我就可以启动我的原生相机视图。

我知道可以用 WKWebview 来完成,但我真的不知道该怎么做。

例如,我的 Javascript (jQuery) 代码可能如下所示:

 // User clicks to start the native camera with Swift
$(".camera_button").click(function() {
    // Function to call the camera view from JS to Swift
});

你能帮我做吗?

谢谢。

原文由 fraxool 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 679
2 个回答

根据@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")
        }
    }

… 最后,我的 Javascript (jQuery) 代码在我的相机按钮上发生点击时(HTML 格式):

 $(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();
    });
});

我希望它能帮助别人:-)!

原文由 fraxool 发布,翻译遵循 CC BY-SA 3.0 许可协议

首先让我们创建一个js文件。在 js 中,当你点击一个元素时,你可以像这样发回消息:

 varmessageToPost = {'ButtonId':'clickMeButton'};
window.webkit.messageHandlers.buttonClicked.postMessage(messageToPost);

创建 js 文件和 wkwebview 后,您需要注入脚本:

   // Setup WKUserContentController instance for injecting user script
  var userController:WKUserContentController= WKUserContentController()

  // Get script that's to be injected into the document
  let js:String= GetScriptFromResources()

  // Specify when and where and what user script needs to be injected into the web document
  var userScript:WKUserScript =  WKUserScript(source: js,
                                         injectionTime: WKUserScriptInjectionTime.AtDocumentEnd
                                         forMainFrameOnly: false)

  // Add the user script to the WKUserContentController instance
  userController.addUserScript(userScript)

  // Configure the WKWebViewConfiguration instance with the WKUserContentController
  webCfg.userContentController= userController;

  //set the message handler
  userController.addScriptMessageHandler(self, name: "buttonClicked")

最后你必须添加监听器功能:

 func userContentController(userContentController: WKUserContentController,
                           didReceiveScriptMessage message: WKScriptMessage) {

        if let messageBody:NSDictionary= message.body as? NSDictionary{
            // Do stuff with messageBody
        }

    }

源代码

原文由 Alex Pelletier 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题