美文网首页ios专题iOS 进阶iOS 深度好文
WKWebView之oc调用js以及WKUserScript J

WKWebView之oc调用js以及WKUserScript J

作者: Crazy2015 | 来源:发表于2017-08-11 11:29 被阅读3595次

    oc调用js就特别简单了,只需WKWebView调用

    • (void)evaluateJavaScript:completionHandler:方法即可。
    /* @abstract Evaluates the given JavaScript string.
     @param javaScriptString The JavaScript string to evaluate.
     @param completionHandler A block to invoke when script evaluation completes or fails.
     @discussion The completionHandler is passed the result of the script evaluation or an error.
    */
    - (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ _Nullable)(_Nullable id, NSError * _Nullable error))completionHandler;
    
    
    WKUserScript JS注入

    在WebKit框架中,我们还可以预先添加JS方法,供其他人员调用。WKUserScript就是帮助我们完成JS注入的类,它能帮助我们在页面填充前或js填充完成后调用。核心方法。

    /*! @abstract Returns an initialized user script that can be added to a @link WKUserContentController @/link.
     @param source The script source.
     @param injectionTime When the script should be injected.
     @param forMainFrameOnly Whether the script should be injected into all frames or just the main frame.
     */
    - (instancetype)initWithSource:(NSString *)source injectionTime:(WKUserScriptInjectionTime)injectionTime forMainFrameOnly:(BOOL)forMainFrameOnly;
    
    #pragma mark - get方法
    - (WKWebView *)webView {
        if (_webView == nil) {
            // js配置
            WKUserContentController *userContentController = [[WKUserContentController alloc] init];
            [userContentController addScriptMessageHandler:self name:@"jsCallOC"];
            // js注入,注入一个alert方法,页面加载完毕弹出一个对话框。
            NSString *javaScriptSource = @"alert(\"WKUserScript注入js\");";
            WKUserScript *userScript = [[WKUserScript alloc] initWithSource:javaScriptSource injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];// forMainFrameOnly:NO(全局窗口),yes(只限主窗口)
            [userContentController addUserScript:userScript];
    
            // WKWebView的配置
            WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
            configuration.userContentController = userContentController;
    
            // 显示WKWebView
            _webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:configuration];
            [self.view addSubview:_webView];
        }
        return _webView;
    }
    

    同时实现代理WKUIDelegate的方法:

    #pragma mark alert弹出框
    - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {
        NSLog(@"%s",__FUNCTION__);
        // 确定按钮
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            completionHandler();
        }];
        // alert弹出框
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:message message:nil preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:alertAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    

    相关文章

      网友评论

      • 丸子_f396:您好,这个方法怎么选择js注入的位置,html要如何识别
        Lazyloading:我也想知道注入的位置 你解决了吗
      • 5dbca6037233:WKUserScript iOS 9.3.0 版本,注入不成功有遇到过么
        亚希路北:@茗玥古城 楼主解决了吗

      本文标题:WKWebView之oc调用js以及WKUserScript J

      本文链接:https://www.haomeiwen.com/subject/pkrlrxtx.html