WKWebView介紹:
1、允許JavaScript的Nitro庫加載并調(diào)用(UIWebView中限制)
2、支持更多的H5特性
3、將UIWebViewDelegate與UIWebView重構(gòu)成了14個(gè)類與3個(gè)協(xié)議
用法:
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
//初始化偏好設(shè)置屬性:preferences
config.preferences = [WKPreferences new];
//The minimum font size in points default is 0;
config.preferences.minimumFontSize = 10;
//是否支持JavaScript
config.preferences.javaScriptEnabled = YES;
//不通過用戶交互,是否可以打開窗口
config.preferences.javaScriptCanOpenWindowsAutomatically = YES;
//通過JS與webView內(nèi)容交互
config.userContentController = [WKUserContentController new];
//注冊的方法
[config.userContentController addScriptMessageHandler:self name:@"js方法名"];
//初始化WKWebView
初始化方法:
WkWebView * wkwebView = [[WKWebView alloc] initWithFrame:frame configuration:config];
self.wkwebView.navigationDelegate = self;
self.wkwebView.UIDelegate = self;
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:str]];
[self.wkwebView loadRequest:request];
WKNavigationDelegate
//在響應(yīng)完成時(shí),調(diào)用的方法。如果設(shè)置為不允許響應(yīng),web內(nèi)容就不會(huì)傳過來
-(void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
decisionHandler(WKNavigationResponsePolicyAllow);
}
//在發(fā)送請求之前,決定是否跳轉(zhuǎn)
-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
decisionHandler(WKNavigationActionPolicyAllow);
}
// 頁面開始加載時(shí)調(diào)用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation;
// 當(dāng)內(nèi)容開始返回時(shí)調(diào)用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation;
OC調(diào)用JS
// 頁面加載完成之后調(diào)用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation;
這個(gè)方法通常處理OC調(diào)用JS的方法
//javaScriptString是JS方法名,completionHandler是異步回調(diào)block
[self.webView evaluateJavaScript:javaScriptString completionHandler:completionHandler];
// 頁面加載失敗時(shí)調(diào)用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation;
ScriptMessageHandler
JS調(diào)用OC
接收到JS的方法名并處理,在使用這個(gè)方法之前一定要在初始化WKWebView的時(shí)候注冊方法名,注冊的方法名一定要和JS端的方法名一樣
-(void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
NSLog(@"%@",message.name);
NSLog(@"%@",message.body);
}
一定要加上這句話
前端在配合使用WKWebView的時(shí)候
window.webkit.messageHandlers.<name>.postMessage(<messageBody>)
//處理內(nèi)存的方法
@interface WeakScriptMessageDelegate : NSObject<WKScriptMessageHandler>
@property (nonatomic, weak) id<WKScriptMessageHandler> scriptDelegate;
- (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate;
@end
@implementation WeakScriptMessageDelegate
- (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate
{
self = [super init];
if (self) {
_scriptDelegate = scriptDelegate;
}
return self;
}
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
[self.scriptDelegate userContentController:userContentController didReceiveScriptMessage:message];
}
@end
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
[userContentController addScriptMessageHandler:[[WeakScriptMessageDelegate alloc] initWithDelegate:self] name:@"closeMe"];
然后在self的dealloc中添加
[[_webView configuration].userContentController removeScriptMessageHandlerForName:@"closeMe"];