在apple API reference中指出在運(yùn)行iOS 8之后的程序時(shí)纺且,使用WKWebView而不是UIWebView添坊。
WebKit框架概覽(Mindnote):
創(chuàng)建
WebKit (WKWebView):
see it in WKWebView API Reference
WKWebView * WK;
WKWebViewConfiguration * config = [[WKWebViewConfiguration alloc]init];
WK = [[WKWebView alloc]initWithFrame:[UIScreen mainScreen].bounds configuration:config];
[WK loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
使用WKWebViewConfiguration來(lái)進(jìn)行配置:
// 1. 設(shè)置線程池
WKProcessPool *pool = [[WKProcessPool alloc]init];
config.processPool = pool;
// WKProcessPool類中沒有暴露任何屬性和方法屉符,配置為同一個(gè)進(jìn)程池的WebView會(huì)共享數(shù)據(jù),例如Cookie、用戶憑證等偿乖,開發(fā)者可以通過(guò)編寫管理類來(lái)分配不同維度的WebView在不同進(jìn)程池中。
// 2. 進(jìn)行偏好設(shè)置
WKPreferences * preference = [[WKPreferences alloc]init];
//最小字體大小 當(dāng)將javaScriptEnabled屬性設(shè)置為NO時(shí)哲嘲,可以看到明顯的效果
preference.minimumFontSize = 0;
//設(shè)置是否支持javaScript 默認(rèn)是支持的
preference.javaScriptEnabled = YES;
//設(shè)置是否允許不經(jīng)過(guò)用戶交互由javaScript自動(dòng)打開窗口
preference.javaScriptCanOpenWindowsAutomatically = YES;
config.preferences = preference;
// 3. **設(shè)置內(nèi)容交互控制器 用于處理JavaScript與native交互**
WKUserContentController * userController = [[WKUserContentController alloc]init];
//設(shè)置處理代理并且注冊(cè)要被JS調(diào)用的方法名稱
[userController addScriptMessageHandler:self name:@"name"];
//JS注入贪薪,注入一個(gè)測(cè)試方法。
NSString *javaScriptSource = @"function userFunc(){window.webkit.messageHandlers.name.postMessage( {/"name/":/"HS/"})}";
WKUserScript *userScript = [[WKUserScript alloc] initWithSource:javaScriptSource injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];// forMainFrameOnly:NO(全局窗口)眠副,yes(只限主窗口)
[userController addUserScript:userScript];
config.userContentController = userController;
OC和JS交互example
1. 執(zhí)行html 已經(jīng)存在的JS方法
- (IBAction)exeFuncTouched:(id)sender {
[self.myWebView evaluateJavaScript:@"showAlert('hahahha')" completionHandler:^(id item, NSError * _Nullable error) {
}];
}
2. JS調(diào)用OC
function callOC(func,param){
var url= "func=" + func;
for(var i in param)
{
url = url + "&" + i + "=" + param[i];
}
window.webkit.messageHandlers.Native.postMessage(url);
}
// JavaScript調(diào)用
<input type="button" value="打個(gè)招呼" onclick="callOC('alert',{'message':'你好么'})" />
Objective-C實(shí)現(xiàn)(WKUserContentController
對(duì)象提供的JavaScript來(lái)發(fā)布消息和注入用戶腳本網(wǎng)頁(yè)視圖方式)
// 通過(guò)
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
config.userContentController = [[WKUserContentController alloc] init];
// 注入JS對(duì)象Native画切,
// 聲明WKScriptMessageHandler 協(xié)議
[config.userContentController addScriptMessageHandler:self name:@"Native"];
self.myWebView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];
self.myWebView.UIDelegate = self;
[self.view addSubview:self.myWebView]
#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message {
if ([message.name isEqualToString:@"Native"]) {
NSLog(@"message.body:%@", message.body);
//如果是自己定義的協(xié)議, 再截取協(xié)議中的方法和參數(shù), 判斷無(wú)誤后在這里手動(dòng)調(diào)用oc方法
NSMutableDictionary *param = [self queryStringToDictionary:message.body];
NSLog(@"get param:%@",[param description]);
NSString *func = [param objectForKey:@"func"];
//調(diào)用本地函數(shù)
if([func isEqualToString:@"alert"])
{
[self showMessage:@"來(lái)自網(wǎng)頁(yè)的提示" message:[param objectForKey:@"message"]];
}
}
}