UIWebView and JavaScriptInterface in Swift

后端 未结 2 1423
花落未央
花落未央 2020-12-15 08:43

How can I to create the JavascriptInterface channel from my web site to my UIWebView?

Example in Android:

webView.addJavascriptInterface(new WebAppIn         


        
2条回答
  •  北海茫月
    2020-12-15 08:59

    Here's a quick example:

    1. Register a URL Scheme such as foobar in Xcode
    2. Handle this URL Scheme in your web view

      func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
          if request.URL?.query?.containsString("show_activity_indicator=true") {
              myActivityIndicator.startAnimating()
          }
      }
      
    3. Finally, call this from your JavaScript

      // Show your activity indicator from JavaScript.
      window.location = "foobar://fizz?show_activity_indicator=true"
      

    Note: See my question here for more information on web view communication in iOS.

提交回复
热议问题