基礎(chǔ)用法很簡(jiǎn)單,在.h文件中遵從代理協(xié)議WKUIDelegate,WKNavigationDelegate

在.m中初始化

切記?
webView.UIDelegate = self;
webView.navigationDelegate = self;
這兩步。
目前我方項(xiàng)目主要用這個(gè)來加載網(wǎng)頁,所用的代理方法主要有5個(gè):
第一個(gè):網(wǎng)頁加載成功
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
}
第二個(gè):網(wǎng)頁加載失敗
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation {
}
第三個(gè):收到響應(yīng)后是否允許網(wǎng)頁跳轉(zhuǎn)
- (void)webView:(WKWebView *)w decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSString *urlStr = navigationAction.request.URL.absoluteString;
NSLog(@"webview urlStr = %@",urlStr);
if (navigationAction.targetFrame == nil) {
[webView loadRequest:navigationAction.request];
}
decisionHandler(WKNavigationActionPolicyAllow);
}
第四個(gè):捕捉Alert提示框并顯示
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(nonnull NSString *)message initiatedByFrame:(nonnull WKFrameInfo *)frame completionHandler:(nonnull void (^)(void))completionHandler {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:message message:nil preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
completionHandler();
}]];
[self presentViewController:alertController animated:YES completion:^{
}];
}
第五個(gè):捕捉Confirm提示框并顯示
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler {
//? js 里面的alert實(shí)現(xiàn),如果不實(shí)現(xiàn),網(wǎng)頁的alert函數(shù)無效? ,
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:message
message:nil
preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"確定"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
completionHandler(YES);
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"取消"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action){
completionHandler(NO);
}]];
[self presentViewController:alertController animated:YES completion:^{}];
}
WKWebView的基本用法是這個(gè),其他的還在研究中……