美文网首页
iOS 原生和H5的交互

iOS 原生和H5的交互

作者: 哥只是个菜鸟 | 来源:发表于2020-07-15 17:47 被阅读0次
    #import <WebKit/WebKit.h>
    #import <WebKit/WKWebViewConfiguration.h>
    #import "FatherViewController.h"
    
    @interface FatherViewController ()<WKScriptMessageHandler,WKNavigationDelegate,WKUIDelegate>{
        WKWebView *_wkWebView;
    }
    
    @end
    
    @implementation FatherViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
        config.preferences = [[WKPreferences alloc] init];
        config.preferences.minimumFontSize = 10;
        config.preferences.javaScriptEnabled = YES;
        config.preferences.javaScriptCanOpenWindowsAutomatically = NO;
        config.userContentController = [[WKUserContentController alloc] init];
        config.processPool = [[WKProcessPool alloc] init];
        //在创建wkWebView时,需要将被js调用的方法注册进去,oc与js端对应实现
        [config.userContentController addScriptMessageHandler:self name:@"callFunciton"];//JS调OC
        [_wkWebView evaluateJavaScript:@"show()" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
          //TODO:OC调用JS
         }];
        WKWebView *wkWebView = [[WKWebView alloc]initWithFrame:self.view.frame configuration:config];
        _wkWebView = wkWebView;
        _wkWebView.navigationDelegate = self;
        _wkWebView.UIDelegate = self;
        NSURLRequest *request = [[NSURLRequest alloc]initWithURL:@""];
        [wkWebView loadRequest:request];
        [self.view addSubview:wkWebView];
    }
    
    #pragma mark --WKScriptMessageHandler JS调用OC的回调方法
    - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
        if ([message.name isEqualToString:@"callFunction"]) {
            //调用原生扫码
        }
    }
    #pragma mark --WKNavigationDelegate 获取当前页面url
    - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
      NSString *url = navigationAction.request.URL.absoluteString;
        if(![url isEqualToString:@"xxxxx"]) {
        // 页面跳转
       }
       decisionHandler(WKNavigationActionPolicyAllow);
    }
    
    #pragma mark -- 监听H5页面的标题和进度条等等
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
    {
        if ([keyPath isEqualToString:@"title"]) {
            if (object == _wkWebView) {
                if(self.navigationController)
                    self.navigationItem.title = _wkWebView.title;
            }
        }
        else {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    }
    - (void)dealloc{
        [_wkWebView.configuration.userContentController removeScriptMessageHandlerForName:@"callFunction"];
        [_wkWebView removeObserver:self forKeyPath:@"title"];
    }
    
    /*
    #pragma mark - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */
    
    @end
    
    #pragma mark --WKUIDelegate 原生页面显示JS的弹出框
    /// 创建一个新的WebView
    - (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures;
    /// 输入框
    - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler;
    /// 确认框
    - (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler;
    /// 警告框
    - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler;
    

    相关文章

      网友评论

          本文标题:iOS 原生和H5的交互

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