1.初始化
_webView = [[WKWebView alloc]initWithFrame:CGRectMake(0,0, screen_width, screen_height)];
_webView.scrollView.bounces = NO;
_webView.navigationDelegate = self;//生命周期,控制页面跳转的代理,最常用
_webView.UIDelegate = self;//不常用
_webView.scrollView.scrollEnabled = NO;
[self.view addSubview:_webView];
2.加载页面
1)加载HTML:[_webView loadHTMLString:htmlString baseURL:nil];
2)加载NSURL:[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
3)加载NSData : - (void)loadData:(NSData *)data MIMEType:(NSString
*)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL
*)baseURL;
3.代理方法
/*************追踪加载过程**********************/
#pragma mark - WKNavigationDelegate
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
[SVProgressHUD show];
[SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeNone];
}
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
[SVProgressHUD dismiss];
}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
[SVProgressHUD dismiss];
}
/*********页面跳转的代理方法有三种,分为(收到跳转与决定是否跳转两种)***/
// 接收到服务器跳转请求之后调用
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
}
// 在收到响应后,决定是否跳转
-
(void)webView:(WKWebView *)webView
decidePolicyForNavigationResponse:(WKNavigationResponse
*)navigationResponse decisionHandler:(void
(^)(WKNavigationResponsePolicy))decisionHandler{
//允许跳转
decisionHandler(WKNavigationResponsePolicyAllow);
//不允许跳转
//decisionHandler(WKNavigationResponsePolicyCancel);
}
// 在发送请求之前,决定是否跳转
-
(void)webView:(WKWebView *)webView
decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
//允许跳转
decisionHandler(WKNavigationActionPolicyAllow);
//不允许跳转
//decisionHandler(WKNavigationActionPolicyCancel);
}
#pragma mark - WKUIDelegate
// 创建一个新的WebView
-
(WKWebView *)webView:(WKWebView *)webView
createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration
forNavigationAction:(WKNavigationAction *)navigationAction
windowFeatures:(WKWindowFeatures *)windowFeatures{
return [[WKWebView alloc]init];
}
// 输入框
-
(void)webView:(WKWebView *)webView
runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt
defaultText:(nullable NSString *)defaultText
initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void
(^)(NSString * __nullable result))completionHandler{
completionHandler(@"http");
}
// 确认框
-
(void)webView:(WKWebView *)webView
runJavaScriptConfirmPanelWithMessage:(NSString *)message
initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL
result))completionHandler{
completionHandler(YES);
}
// 警告框
-
(void)webView:(WKWebView *)webView
runJavaScriptAlertPanelWithMessage:(NSString *)message
initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void
(^)(void))completionHandler{
NSLog(@"%@",message);
completionHandler();
}