1. <small id='xf0ql'></small><noframes id='xf0ql'>

  2. <legend id='xf0ql'><style id='xf0ql'><dir id='xf0ql'><q id='xf0ql'></q></dir></style></legend>
    • <bdo id='xf0ql'></bdo><ul id='xf0ql'></ul>

      <tfoot id='xf0ql'></tfoot>
      <i id='xf0ql'><tr id='xf0ql'><dt id='xf0ql'><q id='xf0ql'><span id='xf0ql'><b id='xf0ql'><form id='xf0ql'><ins id='xf0ql'></ins><ul id='xf0ql'></ul><sub id='xf0ql'></sub></form><legend id='xf0ql'></legend><bdo id='xf0ql'><pre id='xf0ql'><center id='xf0ql'></center></pre></bdo></b><th id='xf0ql'></th></span></q></dt></tr></i><div id='xf0ql'><tfoot id='xf0ql'></tfoot><dl id='xf0ql'><fieldset id='xf0ql'></fieldset></dl></div>

      使用 UIWebview 使用 javascript 调用 swift 函数

      时间:2023-10-21

        <i id='V1cDz'><tr id='V1cDz'><dt id='V1cDz'><q id='V1cDz'><span id='V1cDz'><b id='V1cDz'><form id='V1cDz'><ins id='V1cDz'></ins><ul id='V1cDz'></ul><sub id='V1cDz'></sub></form><legend id='V1cDz'></legend><bdo id='V1cDz'><pre id='V1cDz'><center id='V1cDz'></center></pre></bdo></b><th id='V1cDz'></th></span></q></dt></tr></i><div id='V1cDz'><tfoot id='V1cDz'></tfoot><dl id='V1cDz'><fieldset id='V1cDz'></fieldset></dl></div>
          <tbody id='V1cDz'></tbody>
        <tfoot id='V1cDz'></tfoot>

            <bdo id='V1cDz'></bdo><ul id='V1cDz'></ul>

              • <legend id='V1cDz'><style id='V1cDz'><dir id='V1cDz'><q id='V1cDz'></q></dir></style></legend>

                <small id='V1cDz'></small><noframes id='V1cDz'>

              • 本文介绍了使用 UIWebview 使用 javascript 调用 swift 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                如果有人问过这个问题,我深表歉意,但我找不到任何答案或解决方案.我希望从UIWebView"调用一个 JavaScript 函数并迅速收听它.我发现的任何示例都使用WKWebView".必须有一种简单的方法来监听 JavaScript 函数,如下所示:

                I apologize if this has been asked but I was unable to find any answer or solution. I am looking to call a JavaScript function from a "UIWebView" and listen for it in swift. Any example I have found uses "WKWebView". There has to be an easy way to listen to a JavaScript function like the below:

                // HTML / JS
                <div id ="myDiv" onclick="listenInSwift()">myItem</div>
                

                这可以通过 UIWebView 实现吗?谢谢大家!

                Is this possible with a UIWebView? Thanks all!

                推荐答案

                像这样实现 listenInSwift:

                function listenInSwift() {
                    window.location = 'yoururlscheme://somehost?greeting=hello'
                } 
                

                然后在您的 UIWebViewDelegate 类中使用此代码侦听此 URL:

                Then listen for this URL with this code in your UIWebViewDelegate class:

                func webView(_ webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType navigationType: UIWebViewNavigationType) -> Bool {
                    if request.URL.scheme == 'yoururlscheme' {
                        print('Hello JavaScript...')
                    }
                }
                

                不要忘记在 Xcode 中注册您的 URL Scheme(在本例中为yoururlscheme").

                Don't forget to register your URL Scheme (in this case 'yoururlscheme') in Xcode.

                要在 web 视图中加载本地文件,试试这个:

                To load a local file in the web view, try this:

                let baseURL = NSURL.fileURLWithPath(NSBundle.mainBundle().bundlePath);
                let relativePath = "www/(file)"
                let fileURL = NSURL(string: relativePath, relativeToURL: baseURL);
                let URLRequest = NSURLRequest(URL: fileURL!);
                webView.loadRequest(URLRequest)
                

                这篇关于使用 UIWebview 使用 javascript 调用 swift 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在 iOS 6 上找到 UIWebView 工具栏(以替换它们)? 下一篇:显示进度条,直到它在 UIWebView IOS7 中加载数据

                相关文章

                <legend id='TUuS7'><style id='TUuS7'><dir id='TUuS7'><q id='TUuS7'></q></dir></style></legend>
              • <tfoot id='TUuS7'></tfoot>

                  <bdo id='TUuS7'></bdo><ul id='TUuS7'></ul>

                1. <small id='TUuS7'></small><noframes id='TUuS7'>

                  <i id='TUuS7'><tr id='TUuS7'><dt id='TUuS7'><q id='TUuS7'><span id='TUuS7'><b id='TUuS7'><form id='TUuS7'><ins id='TUuS7'></ins><ul id='TUuS7'></ul><sub id='TUuS7'></sub></form><legend id='TUuS7'></legend><bdo id='TUuS7'><pre id='TUuS7'><center id='TUuS7'></center></pre></bdo></b><th id='TUuS7'></th></span></q></dt></tr></i><div id='TUuS7'><tfoot id='TUuS7'></tfoot><dl id='TUuS7'><fieldset id='TUuS7'></fieldset></dl></div>