原著:https://galileioo.github.io/posts/UIWebview-JS.html
感謝原文作者提供這么好的文章!
------------------------正文如下------------------------
問題描述
html里面的script標(biāo)簽里面需要在網(wǎng)頁加載的時(shí)候直接調(diào)用客戶端的方法。無法調(diào)用成功。
問題分析
之前也做過js通過UIWebview調(diào)用iOS端代碼??梢猿晒φ{(diào)用。但是為什么能夠成功調(diào)用? 因?yàn)槲抑暗淖龇ㄊ钱?dāng)頁面加載成功時(shí)候才去注入wap_obj,代碼如下。
#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
self.jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
self.jsContext[@"wap_obj"] = self;
}
以前的使用場景是點(diǎn)擊webview上的按鈕,觸發(fā)js方法來調(diào)用iOS本地方法。因?yàn)辄c(diǎn)擊按鈕時(shí)候網(wǎng)頁已經(jīng)加載完了。所以這個(gè)場景是ok的。
現(xiàn)在,新的場景是先加載js直接調(diào)用方法。調(diào)用方法在執(zhí)行webViewDidFinishLoad之前。所以方法不觸發(fā)就是理所當(dāng)然。
解決方案
在網(wǎng)上搜索良久,終于找到了一篇文章。JavaScript和Objective-C交互的那些事.里面用到了一個(gè)第三方庫。UIWebView-TS_JavaScriptContext. 參考此項(xiàng)目的寫法,將文件加入工程中之后就可以解決網(wǎng)頁加載中無法成功調(diào)用本地方法的問題。但是看到文章后面的一些評(píng)論說是有被拒的風(fēng)險(xiǎn)。然后去github看了下issue有如下被拒的原因.
I hope that we do not use this library, the bloody lessons.
2016年9月1日 上午1:22
發(fā)件人 Apple
Performance - 2.5.1
Your app uses or references the following non-public APIs:
“parentFrame”
The use of non-public APIs is not permitted on the App Store because it can lead to a poor user experience should these APIs change.
為了防止被拒。我還是放棄此種方法把。
但是上一種方法給出了一種思路。就是使用didCreateJavaScriptContext。這個(gè)方法會(huì)多次回調(diào)。以獲取最新的的JSContext。第一種方法只要不調(diào)用parentFrame方法不就好了?所以對(duì)之前方法加以修改就是改為發(fā)通知方式。方法代碼如下
#import "NSObject+JSContextTracker.h"@implementationNSObject(JSContextTracker)-(void)webView:(id)unuseddidCreateJavaScriptContext:(JSContext*)ctxforFrame:(id)alsoUnused{if(!ctx)return;[[NSNotificationCenterdefaultCenter]postNotificationName:@"LLCreatJSContex"object:ctx];}@end
在使用的地方添加觀測
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(creatJSContex:) name:@"LLCreatJSContex" object:nil];
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"LLCreatJSContex" object:nil];
}
-(void)creatJSContex:(NSNotification*)noti
{
NSLog(@"%@",noti);
//注意以下代碼如果不在主線程調(diào)用會(huì)發(fā)生閃退。
dispatch_async( dispatch_get_main_queue(), ^{
self.jsContext = [_webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
self.jsContext[@"wap_obj"] = self;
self.jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
context.exception = exceptionValue;
NSLog(@"異常信息:%@", exceptionValue);
};
});
}
運(yùn)用這種思路。也可以實(shí)現(xiàn)成功調(diào)用。并且我們的應(yīng)用已經(jīng)通過審核。
參考