之前一直是使用UIWebView与后台交互,这些天刚接触WKWebView,感觉速度要比UIWebVeiw快很多,所以写了新手教程,供大家参考(刚写文章,还请多多指教)!下面步入正题:
首先导入WebKit.framework:
打开Xcode,Build Phases-->Link Binary With Libraries,点击加号,添加WebKit.framework;
然后在需要使用WKWebView的类里引入头文件WebKit/WebKit.h
在.h文件中引入头文件#import <WebKit/WebKit.h>,然后遵循下协议:WKUIDelegate,WKNavigationDelegate,WKScriptMessageHandler
WKUIDelegate:主要实现了涉及到界面显示的回调方法:如WKWebView的改变和js相关内容
WKNavigationDelegate:主要实现了涉及到导航跳转方面的回调方法
WKScriptMessageHandler:这个协议中包含一个必须实现的方法,这个方法是提高App与web端交互的关键,它可以直接将接收到的JS脚本转为OC
接下来声明一个WKWebView对象,在ViewDidLoad里进行初始化:
webPageView = [[WKWebView alloc]initWithFrame:CGRectMake(0.0f,0.0f,SC_W,SC_H)];
[webPageView setUserInteractionEnabled:YES]; //是否支持交互
webPageView.UIDelegate = self;
webPageView.navigationDelegate = self;
[webPageView setOpaque:YES]; //Opaque为不透明的意思,这里为透明
webPageView.scrollView.bounces = NO; //滚动,反弹效果
[self.view addSubview:webPageView];
接下来实现方法:
#pragma mark -- WKUIDelegate
- (nullable WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures{
//用于创建一个新的webView,这里我没有新建
return nil;
}
//WebVeiw关闭(9.0中的新方法)
- (void)webViewDidClose:(WKWebView *)webView NS_AVAILABLE(10_11, 9_0){}
//显示一个JS的Alert(与JS交互)
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示框" message:message preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
completionHandler();
}]];
}
//弹出一个输入框(与JS交互的)
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler{}
//显示一个确认框(JS的)
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确认框" message:message preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
completionHandler(YES);
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
completionHandler(NO);
}]];
}
#pragma mark -- WKNavigationDelegate
// 在发送请求之前,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
NSString *requestString = navigationAction.request.URL.absoluteString;
NSLog(@"requestString:%@",requestString);
decisionHandler(WKNavigationActionPolicyAllow);
}
// 在收到响应后,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
NSLog(@"收到响应");
decisionHandler(WKNavigationResponsePolicyAllow);
}
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
NSLog(@"开始加载");
}
// 接收到服务器跳转请求之后再执行
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
NSLog(@"接收到服务器跳转请求");
}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
NSLog(@"加载失败");
NSLog(@"error== %@“, error);
}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
NSLog(@"加载完成");
}
#pragma mark -- WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
}
以上是我的学习笔记,难免有不严谨之处,欢迎指正👏👏👏