WKWebView和https以及JS交互的坑

写在前面,平复心态:

//治大国,若烹小鲜,以道莅天下,其鬼不神。非其鬼不神,其神不伤人。非其神不伤人,圣人亦不伤人。夫两不相伤,故德交归焉。

心塞,wkweb遇到了一些问题,忙活半天找不到问题,解决过后却发现很简单,在下面标注:首先,如果之前用的是UIWebView的话,那么不仅iOS前段需要改动,web前段也需要改动。其次,如果之前是用的http转https也有一些注意点。上代码吧:

@interface SignUpInViewController ()
//初始化页面
- (void)initWKWebView
{
    //进行配置控制器
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    //实例化对象
    WKUserContentController* userContentController = [WKUserContentController new];
    //调用JS方法
    [userContentController addScriptMessageHandler:self name:@"login"];
    configuration.userContentController = userContentController;
//    configuration.preferences.javaScriptEnabled = YES;
    
    WKPreferences *preferences = [WKPreferences new];
    preferences.javaScriptCanOpenWindowsAutomatically = YES;
    preferences.minimumFontSize = 0.0;
    preferences.javaScriptEnabled = YES;
    configuration.preferences = preferences;
    
    self.webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:configuration];
    NSURL *fileURL = [NSURL URLWithString:@"https://www.tingting365.com/index11.html"];
    [self.webView loadRequest:[NSURLRequest requestWithURL:fileURL]];
    self.webView.navigationDelegate = self;
    self.webView.UIDelegate = self;
    [self.view addSubview:self.webView];
}

这是WKWebView的初始化,有两个注意点1,WKUserContentController最好重新创建在进行赋值,其二:WKPreferences同理。代理方法不多赘述。

WKWeb和js交互原理网上很多,我这儿实现的方式就一行代码[userContentControlleraddScriptMessageHandler:selfname:@"login"];这里js方法名就是@“login”。放入之后再代理方法里面进行回调和识别(加判断,和login一样),代码如下

#pragma mark - WKScriptMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
{
    //    message.body  --  Allowed types are NSNumber, NSString, NSDate, NSArray,NSDictionary, and NSNull.
    NSLog(@"body:%@",message.body);
    if ([message.name isEqualToString:@"login"]) {
        NSLog(@"哈哈");
    }
}

好了,我心塞的一点来了:我这儿https没有做认证,搞了一个投机的方式:

- (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);
    }
}

oc代码很简单,就是上面两个代理方法就好了,js代码也不麻烦,直接上代码吧:
JS:login方法如下:

if(window.login){
        //   我这儿需要的登录回调方法,可用可不要。
    window.login.call(window,swUser);
}else{
    //alert("login 方法没有为空")
}
//swUser就是call方法
//这一句一定要加,一定要加,一定要加
window.webkit.messageHandlers.login.postMessage(swUser);

17年7月26日更新:
有朋友问我,window.webkit.messageHandlers.login.postMessage(swUser);已经加了,wkweb的代理写的也没有问题,为什么didReceiveScriptMessage代理方法不调用?
原因如下

// 这是固定格式,一定要注意。。'parameter'可替换但是不能省略,一定要传参数,在与web协商的时候要注意,否则OC的didReceiveScriptMessage代理方法不会调用的
window.webkit.messageHandlers.ActionName.postMessage('parameter');

你可能感兴趣的:(WKWebView和https以及JS交互的坑)