WKWebView相对于UIWebView强大了很多,内存的消耗相对少了,所提供的接口也丰富了。
现在谈一谈WKWebView的基本使用
1. navigationDelegate
[objc]view plaincopyprint?
- (void)webView:(WKWebView*)webViewdidStartProvisionalNavigation:(WKNavigation*)navigation {// 类似UIWebView的 -webViewDidStartLoad:
NSLog(@"didStartProvisionalNavigation");
[UIApplicationsharedApplication].networkActivityIndicatorVisible=YES;
}
- (void)webView:(WKWebView*)webViewdidCommitNavigation:(WKNavigation*)navigation {
NSLog(@"didCommitNavigation");
}
- (void)webView:(WKWebView*)webViewdidFinishNavigation:(WKNavigation*)navigation {// 类似 UIWebView 的 -webViewDidFinishLoad:
NSLog(@"didFinishNavigation");
[selfresetControl];
if(webView.title.length>0) {
self.title= webView.title;
}
}
- (void)webView:(WKWebView*)webViewdidFailProvisionalNavigation:(WKNavigation*)navigationwithError:(NSError*)error {
// 类似 UIWebView 的- webView:didFailLoadWithError:
NSLog(@"didFailProvisionalNavigation");
}
- (void)webView:(WKWebView*)webViewdecidePolicyForNavigationResponse:(WKNavigationResponse*)navigationResponsedecisionHandler:(void(^)(WKNavigationResponsePolicy))decisionHandler {
decisionHandler(WKNavigationResponsePolicyAllow);
}
- (void)webView:(WKWebView*)webViewdecidePolicyForNavigationAction:(WKNavigationAction*)navigationActiondecisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler {
// 类似 UIWebView 的 -webView: shouldStartLoadWithRequest: navigationType:
NSLog(@"4.%@",navigationAction.request);
NSString*url = [navigationAction.request.URL.absoluteStringstringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
decisionHandler(WKNavigationActionPolicyAllow);
}
- (void)webView:(WKWebView*)webViewdidReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge*)challengecompletionHandler:(void(^)(NSURLSessionAuthChallengeDisposition,NSURLCredential*))completionHandler {
}
2 UIDelegate
[objc]view plaincopyprint?
- (WKWebView*)webView:(WKWebView*)webViewcreateWebViewWithConfiguration:(WKWebViewConfiguration*)configurationforNavigationAction:(WKNavigationAction*)navigationActionwindowFeatures:(WKWindowFeatures*)windowFeatures {
// 接口的作用是打开新窗口委托
[selfcreateNewWebViewWithURL:webView.URL.absoluteStringconfig:configuration];
returncurrentSubView.webView;
}
- (void)webView:(WKWebView*)webViewrunJavaScriptAlertPanelWithMessage:(NSString*)messageinitiatedByFrame:(WKFrameInfo*)framecompletionHandler:(void(^)())completionHandler
{// js 里面的alert实现,如果不实现,网页的alert函数无效
UIAlertController*alertController = [UIAlertControlleralertControllerWithTitle:message
message:nil
preferredStyle:UIAlertControllerStyleAlert];
[alertControlleraddAction:[UIAlertActionactionWithTitle:@"确定"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction*action) {
completionHandler();
}]];
[selfpresentViewController:alertControlleranimated:YEScompletion:^{}];
}
- (void)webView:(WKWebView*)webViewrunJavaScriptConfirmPanelWithMessage:(NSString*)messageinitiatedByFrame:(WKFrameInfo*)framecompletionHandler:(void(^)(BOOL))completionHandler {
// js 里面的alert实现,如果不实现,网页的alert函数无效 ,
UIAlertController*alertController = [UIAlertControlleralertControllerWithTitle:message
message:nil
preferredStyle:UIAlertControllerStyleAlert];
[alertControlleraddAction:[UIAlertActionactionWithTitle:@"确定"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction*action) {
completionHandler(YES);
}]];
[alertControlleraddAction:[UIAlertActionactionWithTitle:@"取消"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction*action){
completionHandler(NO);
}]];
[selfpresentViewController:alertControlleranimated:YEScompletion:^{}];
}
- (void)webView:(WKWebView*)webViewrunJavaScriptTextInputPanelWithPrompt:(NSString*)promptdefaultText:(NSString*)defaultTextinitiatedByFrame:(WKFrameInfo*)framecompletionHandler:(void(^)(NSString*))completionHandler {
completionHandler(@"Client Not handler");
}
3. WKWebView 执行脚本方法
[objc]view plaincopyprint?
- (void)evaluateJavaScript:(NSString*)javaScriptStringcompletionHandler:(void(^)(id,NSError*))completionHandler;
[objc]view plaincopyprint?
completionHandler 拥有两个参数,一个是返回错误,一个可以返回执行脚本后的返回值
4. WKWebView 的Cookie问题
UIWebView 中会自动保存Cookie,如果登录了一次,下次再次进入的时候,会记住登录状态
而在WKWebView中,并不会这样,WKWebView在初始化的时候有一个方法
[objc]view plaincopyprint?
- (instancetype)initWithFrame:(CGRect)frameconfiguration:(WKWebViewConfiguration*)configuration
通过这个方法,设置 configuration 让WKWebView知道登录状态,configuration 可以通过已有的Cookie进行设置,也可以通过保存上一次的configuration进行设置
参考 stackoverflow上回答:http://stackoverflow.com/questions/26573137/can-i-set-the-cookies-to-be-used-by-a-wkwebview/26577303#26577303
[objc]view plaincopyprint?
WKWebView* webView =/*set up your webView*/
NSMutableURLRequest* request = [NSMutableURLRequestrequestWithURL:[NSURLURLWithString:@"http://example.com/index.html"]];
[requestaddValue:@"TeskCookieKey1=TeskCookieValue1;TeskCookieKey2=TeskCookieValue2;"forHTTPHeaderField:@"Cookie"];
// use stringWithFormat: in the above line to inject your values programmatically
[webViewloadRequest:request];
[objc]view plaincopyprint?
WKUserContentController* userContentController = WKUserContentController.new;
WKUserScript* cookieScript = [[WKUserScriptalloc]
initWithSource:@"document.cookie = 'TeskCookieKey1=TeskCookieValue1';document.cookie = 'TeskCookieKey2=TeskCookieValue2';"
injectionTime:WKUserScriptInjectionTimeAtDocumentStartforMainFrameOnly:NO];
// again, use stringWithFormat: in the above line to inject your values programmatically
[userContentControlleraddUserScript:cookieScript];
WKWebViewConfiguration* webViewConfig = WKWebViewConfiguration.new;
webViewConfig.userContentController= userContentController;
WKWebView* webView = [[WKWebViewalloc]initWithFrame:CGRectMake(/*set your values*/)configuration:webViewConfig];