一、导入依赖库
WebKit
二、创建WebView
//1.
创建配置项
WKWebViewConfiguration *configuration = [[ WKWebViewConfiguration alloc ] init ];
//1.1 设置偏好
configuration. preferences = [[ WKPreferences alloc ] init ];
configuration. preferences . minimumFontSize = 10 ;
configuration. preferences . javaScriptEnabled = YES ;
// 默认是不能通过 JS 自动打开字典的,必须通过用户交互才能打开
configuration. preferences . javaScriptCanOpenWindowsAutomatically = NO ;
//1.2 设置内容交互配置
configuration. userContentController = [[ WKUserContentController alloc ] init ];
//1.2.1 添加一个 JS 到 Html 中,这样就可以直接在 JS 调用我们添加的 JS 方法
WKUserScript *script = [[ WKUserScript alloc ] initWithSource : @"function showAlert()" injectionTime : WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly : YES ];
[configuration. userContentController addUserScript :script];
//2. 创建 WKWebView
WKWebView *browse = [[ WKWebView alloc ] initWithFrame : self . view . frame configuration :configuration];
//2.1 设置代理
browse. navigationDelegate = self ;
browse. UIDelegate = self ;
//2.2 设置需要加载的链接
NSString *urlStr = @"" ;
[browse loadHTMLString :urlStr baseURL : nil ];
[ self . view addSubview :browse];
/*
* goBack 后退
* goForward 前进
* 监听通知: loading 、 title 、 estimatedProgress 动能对应为:是否正在加载中、页面的标题、页面内容加载进度
WKWebViewConfiguration *configuration = [[ WKWebViewConfiguration alloc ] init ];
//1.1 设置偏好
configuration. preferences = [[ WKPreferences alloc ] init ];
configuration. preferences . minimumFontSize = 10 ;
configuration. preferences . javaScriptEnabled = YES ;
// 默认是不能通过 JS 自动打开字典的,必须通过用户交互才能打开
configuration. preferences . javaScriptCanOpenWindowsAutomatically = NO ;
//1.2 设置内容交互配置
configuration. userContentController = [[ WKUserContentController alloc ] init ];
//1.2.1 添加一个 JS 到 Html 中,这样就可以直接在 JS 调用我们添加的 JS 方法
WKUserScript *script = [[ WKUserScript alloc ] initWithSource : @"function showAlert()" injectionTime : WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly : YES ];
[configuration. userContentController addUserScript :script];
//2. 创建 WKWebView
WKWebView *browse = [[ WKWebView alloc ] initWithFrame : self . view . frame configuration :configuration];
//2.1 设置代理
browse. navigationDelegate = self ;
browse. UIDelegate = self ;
//2.2 设置需要加载的链接
NSString *urlStr = @"" ;
[browse loadHTMLString :urlStr baseURL : nil ];
[ self . view addSubview :browse];
/*
* goBack 后退
* goForward 前进
* 监听通知: loading 、 title 、 estimatedProgress 动能对应为:是否正在加载中、页面的标题、页面内容加载进度
*/
三、实现代理
#pragma mark - WKUIDelegate
#pragma mark HTML 中的 alert
- ( void )webView:( WKWebView *)webView runJavaScriptAlertPanelWithMessage:( NSString *)message initiatedByFrame:( WKFrameInfo *)frame completionHandler:( void (^)( void ))completionHandler
{
completionHandler();
}
#pragma mark HTML 中的 confirm
- ( void )webView:( WKWebView *)webView runJavaScriptConfirmPanelWithMessage:( NSString *)message initiatedByFrame:( WKFrameInfo *)frame completionHandler:( void (^)( BOOL ))completionHandler
{
completionHandler( YES );
}
#pragma mark HTML 中的 prompt
- ( void )webView:( WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:( NSString *)prompt defaultText:( NSString *)defaultText initiatedByFrame:( WKFrameInfo *)frame completionHandler:( void (^)( NSString * _Nullable ))completionHandler
{
completionHandler( @"success" );
}
#pragma mark - WKScriptMessageHandler
- ( void )userContentController:( WKUserContentController *)userContentController didReceiveScriptMessage:( WKScriptMessage *)message
{
// 在 js 端通过 window.webkit.messageHandlers.{InjectedName}.postMessage() 方法来发送消息到 native ,通过这个代理可以接收到消息并做相应的处理。
}
#pragma mark - WKNavigationDelegate
#pragma mark 是否允许跳转导航,如果不允许可以手动跳转 [[UIApplication sharedApplication] openURL:@""];
- ( void )webView:( WKWebView *)webView decidePolicyForNavigationAction:( WKNavigationAction *)navigationAction decisionHandler:( void (^)( WKNavigationActionPolicy ))decisionHandler
{
decisionHandler( WKNavigationActionPolicyAllow );
}
#pragma mark 开始加载页面
- ( void )webView:( WKWebView *)webView didStartProvisionalNavigation:( WKNavigation *)navigation
{
}
#pragma mark 决定是否允许导航响应,如果不允许就不会跳转到链接的页面
- ( void )webView:( WKWebView *)webView decidePolicyForNavigationResponse:( WKNavigationResponse *)navigationResponse decisionHandler:( void (^)( WKNavigationResponsePolicy ))decisionHandler
{
decisionHandler( WKNavigationResponsePolicyAllow );
}
#pragma mark 内容开始返回
- ( void )webView:( WKWebView *)webView didCommitNavigation:( WKNavigation *)navigation
{
}
#pragma mark 加载完成
- ( void )webView:( WKWebView *)webView didFinishNavigation:( WKNavigation *)navigation
{
}
#pragma mark 加载失败
- ( void )webView:( WKWebView *)webView didFailNavigation:( WKNavigation *)navigation withError:( NSError *)error
{
}
#pragma mark 如果我们需要处理在重定向时,需要实现下面的代理方法就可以接收到
- ( void )webView:( WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:( WKNavigation *)navigation
{
}
#pragma mark 如果我们的请求要求授权、证书等,我们需要处理下面的代理方法,以提供相应的授权处理等
- ( void )webView:( WKWebView *)webView didReceiveAuthenticationChallenge:( NSURLAuthenticationChallenge *)challenge completionHandler:( void (^)( NSURLSessionAuthChallengeDisposition , NSURLCredential * _Nullable ))completionHandler
{
NSURLCredential *credential = [[ NSURLCredential alloc ] init ];
completionHandler( NSURLSessionAuthChallengeUseCredential ,credential);
}
#pragma mark 当我们终止页面加载时,我们会可以处理下面的代理方法,如果不需要处理,则不用实现之
- ( void )webViewWebContentProcessDidTerminate:( WKWebView *)webView
{
#pragma mark HTML 中的 alert
- ( void )webView:( WKWebView *)webView runJavaScriptAlertPanelWithMessage:( NSString *)message initiatedByFrame:( WKFrameInfo *)frame completionHandler:( void (^)( void ))completionHandler
{
completionHandler();
}
#pragma mark HTML 中的 confirm
- ( void )webView:( WKWebView *)webView runJavaScriptConfirmPanelWithMessage:( NSString *)message initiatedByFrame:( WKFrameInfo *)frame completionHandler:( void (^)( BOOL ))completionHandler
{
completionHandler( YES );
}
#pragma mark HTML 中的 prompt
- ( void )webView:( WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:( NSString *)prompt defaultText:( NSString *)defaultText initiatedByFrame:( WKFrameInfo *)frame completionHandler:( void (^)( NSString * _Nullable ))completionHandler
{
completionHandler( @"success" );
}
#pragma mark - WKScriptMessageHandler
- ( void )userContentController:( WKUserContentController *)userContentController didReceiveScriptMessage:( WKScriptMessage *)message
{
// 在 js 端通过 window.webkit.messageHandlers.{InjectedName}.postMessage() 方法来发送消息到 native ,通过这个代理可以接收到消息并做相应的处理。
}
#pragma mark - WKNavigationDelegate
#pragma mark 是否允许跳转导航,如果不允许可以手动跳转 [[UIApplication sharedApplication] openURL:@""];
- ( void )webView:( WKWebView *)webView decidePolicyForNavigationAction:( WKNavigationAction *)navigationAction decisionHandler:( void (^)( WKNavigationActionPolicy ))decisionHandler
{
decisionHandler( WKNavigationActionPolicyAllow );
}
#pragma mark 开始加载页面
- ( void )webView:( WKWebView *)webView didStartProvisionalNavigation:( WKNavigation *)navigation
{
}
#pragma mark 决定是否允许导航响应,如果不允许就不会跳转到链接的页面
- ( void )webView:( WKWebView *)webView decidePolicyForNavigationResponse:( WKNavigationResponse *)navigationResponse decisionHandler:( void (^)( WKNavigationResponsePolicy ))decisionHandler
{
decisionHandler( WKNavigationResponsePolicyAllow );
}
#pragma mark 内容开始返回
- ( void )webView:( WKWebView *)webView didCommitNavigation:( WKNavigation *)navigation
{
}
#pragma mark 加载完成
- ( void )webView:( WKWebView *)webView didFinishNavigation:( WKNavigation *)navigation
{
}
#pragma mark 加载失败
- ( void )webView:( WKWebView *)webView didFailNavigation:( WKNavigation *)navigation withError:( NSError *)error
{
}
#pragma mark 如果我们需要处理在重定向时,需要实现下面的代理方法就可以接收到
- ( void )webView:( WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:( WKNavigation *)navigation
{
}
#pragma mark 如果我们的请求要求授权、证书等,我们需要处理下面的代理方法,以提供相应的授权处理等
- ( void )webView:( WKWebView *)webView didReceiveAuthenticationChallenge:( NSURLAuthenticationChallenge *)challenge completionHandler:( void (^)( NSURLSessionAuthChallengeDisposition , NSURLCredential * _Nullable ))completionHandler
{
NSURLCredential *credential = [[ NSURLCredential alloc ] init ];
completionHandler( NSURLSessionAuthChallengeUseCredential ,credential);
}
#pragma mark 当我们终止页面加载时,我们会可以处理下面的代理方法,如果不需要处理,则不用实现之
- ( void )webViewWebContentProcessDidTerminate:( WKWebView *)webView
{
}