標籤:style blog http color io os ar java sp
背景:
UIWebView: iOS 用來展示 web 端內容的控制項。
1. 核心方法:
- (NSString*)stringByEvaluatingJavaScriptFromString:(NSString *)script;
script 就是 JS 代碼,返回結果為 js 執行結果。 比如一個 JS function 為
function testFunction(abc){ return abc;};
webview 調用此 JS 代碼如下:
NSString *js = @"testFunction(‘abc‘)";NSString *result = [webView stringByEvaluatingJavaScriptFromString:js];
2. 重要回調:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
webview 每當需要去載入一個 request 就先回調這個方法,讓上層決定是否 載入。一般在這裡截獲,進行本地的處理。
Native 調用 JS:
本質就一個方法,通過 stringByEvaluatingJavaScriptFromString,都是同步。
下面重點說說JS怎麼回調Native:
1.通常方法:js修通過改doucument的loaction或者建立一個看不見的iFrame,修改它的 src,就會觸發回調 webView 的 shouldStartLoadWithRequest,參數 request 的 url 就是新賦值的 location 或者 url,上層截獲這個 url 的參數,對此分發即可。 這個都是非同步呼叫的。
如 JS function:
var messagingIframe; messagingIframe = document.createElement(‘iframe‘); messagingIframe.style.display = ‘none‘; document.documentElement.appendChild(messagingIframe);
function TestIOSJS(){ messagingIframe.src = "ios/test/click"; };
當觸發上面的JS時,webview會收到下面的回調:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ NSString *url = request.URL.absoluteString; if([url hasSuffix:@"ios/test/click"]){ //do something you want return NO; } return YES;}
通過截獲這個request的參數就可以做native需要做的事情。
有個開源的代碼挺不錯的,大家可以看看:https://github.com/marcuswestin/WebViewJavascriptBridge
2.通過XMLHttpRequest:
(1) Native子類化一個NSURLProtocol類,並通過[NSURLProtocol registerClass:self];把自己註冊。
(2) JS function 建立一個 XMLHttpRequest 對象,然後可以設定攜帶的參數,設定同步或者非同步,然後通過 send 發送請求。
function iOSExec(){ var execXhr = new XMLHttpRequest(); execXhr.open(‘HEAD‘, "/!test_exec?" + (+new Date()), true); //設定scheme var vcHeaderValue = /.*\((.*)\)/.exec(navigator.userAgent)[1]; execXhr.setRequestHeader(‘vc‘, vcHeaderValue);//設定參數等 execXhr.setRequestHeader(‘rc‘, 1); // 發起請求 execXhr.send(null); };
(3) 因為步驟1已經把自己註冊,所以每個用戶端的網路請求都會請求這個類 的+(BOOL)canInitWithRequest:(NSURLRequest *)request,讓此決定是否需要產生這個request。
@implementation TestURLProtocol+(void)initProtocol{ [NSURLProtocol registerClass:self];}+(BOOL)canInitWithRequest:(NSURLRequest *)request{ NSString *url = request.URL.absoluteString; if([url containsString:@"!test_exec"]){ //do something } return NO;}
(4) 通過擷取這個request的參數,上層可以進行攔截,然後進行本地的相 關操作。
這個方法比較少用,不過能解決JS同步回調Native的方法。
這裡也有一個開源庫,大家可以看一下:https://github.com/apache/cordova-ios/tree/master/CordovaLib
The End.
iOS: JS和Native互動的兩種方法