iOS WKWebView与JS交互

1.OC调用JS方法

    NSString *shareMethod = @"share('参数1')";
    [self.webView evaluateJavaScript:shareMethod completionHandler:^(id _Nullable objStr, NSError * _Nullable error) {
    }];

2.JS调用OC的方法

1.初始化WKWebView时,调用addScriptMessageHandler:name:方法,name为js中的方法名

  - (void)setupWKWebView {
    //cookie
    WKCookieSyncManager *manager = [WKCookieSyncManager sharedWKCookieSyncManager];
    cookieStr = [manager setCookieWithIsCustomMsg:_chatCustomMsgBool];
    WKUserScript *cookieScript = [[WKUserScript alloc] initWithSource:cookieStr injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO];
    //初始化WKWebViewConfiguration
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    //方法集合
    ocAndJSMethodArr = [Helper jsCallOCMethodArr];
    //初始化WKUserContentController
    userContentController = [[WKUserContentController alloc] init];
    for (NSString *methodStr in ocAndJSMethodArr) {//注册供js调用的方法
        [userContentController addScriptMessageHandler:self name:methodStr];
    }
    configuration.preferences.javaScriptCanOpenWindowsAutomatically = YES;
    [userContentController addUserScript:cookieScript];
    configuration.userContentController = userContentController;
    configuration.preferences.javaScriptEnabled = YES;
 //初始化WKWebView
    _webView = [[WKWebView alloc] initWithFrame:self.view.bounds) configuration:configuration]; 
    _webView.navigationDelegate = self;
    _webView.UIDelegate = self;
    [self.view addSubview:_webView];
    
    _mutRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:_urlStr] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15];
    //设置cookie
    NSDictionary *cookiesDcit = [CustomeSetWebViewCookie setWebViewCookieWithURL:_urlStr isCustomMsg:NO];
    [_mutRequest setValue: [cookiesDcit objectForKey:@"Cookie"] forHTTPHeaderField: @"Cookie"];

h5:类似如下代码

window.webkit.messageHandlers.share.postMessage() 

2.实现WKScriptMessageHandler代理方法,当js调用scan方法时,会回调此代理方法:

//加载页面数据完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    DLog(@"加载页面完成"); 
    NSMutableString *mutMethodStr = [[NSMutableString alloc] init];
    //由于h5那边没有按照类似于“window.webkit.messageHandlers.share.postMessage() ”的语法处理,以及为了JS通过类似于“Test_APP”的对象调用OC方法,所以通过注入一段以下这样的JS代码给网页以便本地调用
    for (int i=0; i

注:页面销毁时,需要移除掉所调用的方法

- (void)removeScriptMessage {
    //注册的方法要在view dealloc前 remove 否则 不走 dealloc,会循环引用
    for (NSString *nameStr in ocAndJSMethodArr) {
        [userContentController removeScriptMessageHandlerForName:nameStr];
    }
}

你可能感兴趣的:(iOS WKWebView与JS交互)