引入#import <WebKit/WebKit.h>
//初期化加载
self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT)];
NSURLRequest*requestURL = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.baidu.com"]];
self.webView.backgroundColor = [UIColor whiteColor];
self.webView.UIDelegate = self;
self.webView.navigationDelegate = self;
[self.webView loadRequest:requestURL];
[self.view addSubview:self.webView];
//初期化网页调用原生的方法
-(void)initJS
{
WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc]init];
WKUserContentController * userContentController =[[WKUserContentController alloc]init];
[userContentController addScriptMessageHandler:self name:@"testFuction"];
configuration.userContentController = userContentController;
configuration.preferences.javaScriptEnabled = YES;
}
#pragma mark - WKUIDelegate
//当用户点击网页上的链接,需要打开新页面时,将先调用这个方法。
-(WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
if (!navigationAction.targetFrame.isMainFrame) {
[webView loadRequest:navigationAction.request];
}
return nil;
}
#pragma mark - WKNavigationDelegate
/**
* 页面开始加载时调用
*
* @param webView 实现该代理的webview
* @param navigation 当前navigation
*/
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
DLog(@"%s", __FUNCTION__);
}
/**
* 当内容开始返回时调用
*
* @param webView 实现该代理的webview
* @param navigation 当前navigation
*/
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
DLog(@"%s", __FUNCTION__);
}
/**
* 页面加载完成之后调用
*
* @param webView 实现该代理的webview
* @param navigation 当前navigation
*/
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
__weak WebViewController * selfController = self;
[webView evaluateJavaScript:@"document.title" completionHandler:^(NSString * _Nullable title, NSError * _Nullable error) {
NSLog(@"%@",title);//读取网页的头名
}];
//初期化网页调用原生的方法
[self initJS];
//调用页面JS方法向h5页面传值
NSSting * a = @"haha";
NSString *passA = [NSString stringWithFormat:@"getInfo('%@');", a];
[webView evaluateJavaScript:passUidStr completionHandler:nil];
}
/**
* 加载失败时调用
*
* @param webView 实现该代理的webview
* @param navigation 当前navigation
* @param error 错误
*/
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
DLog(@"%s", __FUNCTION__);
}
/**
* 接收到服务器跳转请求之后调用
*
* @param webView 实现该代理的webview
* @param navigation 当前navigation
*/
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation {
DLog(@"%s", __FUNCTION__);
}
/**
* 在收到响应后,决定是否跳转
*
* @param webView 实现该代理的webview
* @param navigationResponse 当前navigation
* @param decisionHandler 是否跳转block
*/
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
// 如果响应的地址是百度,则允许跳转
if ([navigationResponse.response.URL.host.lowercaseString isEqual:@"www.baidu.com"]) {
// 允许跳转
decisionHandler(WKNavigationResponsePolicyAllow);
return;
}
// 不允许跳转
decisionHandler(WKNavigationResponsePolicyCancel);
}
/**
* 在发送请求之前,决定是否跳转
*
* @param webView 实现该代理的webview
* @param navigationAction 当前navigation
* @param decisionHandler 是否调转block
*/
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
if ([navigationAction.request.URL.host.lowercaseString isEqual:@"www.baidu.com"]) {
// 允许跳转
decisionHandler(WKNavigationActionPolicyAllow);
return;
}
// 不允许跳转
decisionHandler(WKNavigationActionPolicyCancel);
}
//跳过证书方法
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
NSURLCredential *card = [[NSURLCredential alloc]initWithTrust:challenge.protectionSpace.serverTrust];
completionHandler(NSURLSessionAuthChallengeUseCredential,card);
}
}
#pragma mark WKScriptMessageHandler
//页面调用原生的方法的回调方法
-(void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
if ([message.name isEqualToString:@"testFuction"]){
//接收页面传来的参数
NSArray *args = [JSContext currentArguments];
JSValue *aaValue = args[0];
JSValue *bbValue = args[1];
JSValue *ccValue = args[2];
NSString *aa = [NSString stringWithFormat:@"%@", aaValue];
NSString *bb = [NSString stringWithFormat:@"%@", bbValue];
NSString *cc = [NSString stringWithFormat:@"%@", ccValue];
//Todo 根据参数做对应响应处理
}
}