2016-05-20 146 views
3

如何從我的網站創建JavascriptInterface通道到我的UIWebView?Swift中的UIWebView和JavaScriptInterface

實例安卓

webView.addJavascriptInterface(new WebAppInterface(this), "Android"); 

而從這個JavascriptInterface我想提請方法,例如:

func webViewDidStartLoad(webView: UIWebView) 

myActivityIndicator.startAnimating() 

我該怎麼辦?

回答

7

對於WKWebView:源here

的JavaScript

function callNativeApp() { 
    try { 
      webkit.messageHandlers.callbackHandler.postMessage("Hello from JavaScript"); 
    } catch(err) { 
      console.log('The native context does not exist yet'); 
    } 
} 

夫特

import WebKit 
class ViewController: UIViewController, WKScriptMessageHandler { 

    @IBOutlet var containerView: UIView? = nil 

    var webView: WKWebView? 

    override func loadView() { 
     super.loadView() 

     let contentController = WKUserContentController() 
     contentController.addScriptMessageHandler(self, name: "callbackHandler") 
     let config = WKWebViewConfiguration() 
     config.userContentController = contentController 

     self.webView = WKWebView(frame: self.containerView!.bounds, configuration: config) 
     self.view = self.webView 

    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     //I use the file html in local 
     let path = NSBundle.mainBundle().pathForResource("index", ofType: "html") 
     let url = NSURL(fileURLWithPath: path!) 
     let req = NSURLRequest(URL: url) 
     self.webView!.loadRequest(req) 

    } 

    fun userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) { 
     if (message.name == "callbackHandler"){ 
      print("\(message.body)") 
     } 
    } 

} 

對於UIWebView中:源here

中的JavaScript HTML

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
    <script> 
     (function(){ 
      $(window).load(function(){ 

       $('.clickMe').on('click', function(){ 
        window.location = "foobar://fizz?Hello_from_javaScript"; 
       }); 
      }); 
     })(jQuery); 
    </script> 

斯威夫特

import UIKit 
class ViewController: UIViewController, UIWebViewDelegate { 

    @IBOutlet weak var Web: UIWebView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

    let path = NSBundle.mainBundle().pathForResource("index", ofType: "html") 
     let url = NSURL(fileURLWithPath: path!) 
     let req = NSURLRequest(URL: url) 
     Web.delegate = self 
     Web.loadRequest(req) 
    } 

    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool { 
     if request.URL?.query != nil { 
      print("\(request.URL!.query!)") 
     } 
     return true 
    } 

} 
+0

如何加載本地HTML文件中WKWebView? – Raghuram

4

這裏有一個簡單的例子:

  1. 註冊一個URL方案,如在Xcode
  2. foobar
  3. 處理您的Web視圖此URL方案

    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool { 
        if request.URL?.query?.containsString("show_activity_indicator=true") { 
         myActivityIndicator.startAnimating() 
        } 
    } 
    
  4. 最後,調用此您的JavaScript

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

注意:有關iOS中的Web視圖通信的更多信息,請參閱my question here