iOS开发

iOS WKWebView的自用

2021-05-01  本文已影响0人  小和大大

自己项目调试的,实际使用按照自己逻辑处理。iosClientApi为与前端协议的方法名,可以注册多个,但是需要离开页面时候销毁,否则会引起循环引用问题。我自己逻辑是交互方法较多,所以用参数形式才处理更多的业务逻辑,根据method判断方法,param处理参数。每次收到方法后都需要调用js告诉前端是否收到,evaluateJsMethod方法需要前端告诉需要参数和调用格式,例如我的M.echo.push("m1", {a:1, b:3})。

- (void)viewDidLoad {
    [super viewDidLoad];
    
    DDLog(@"--网页加载地址---webUrl:%@", self.urlString);
    [self layoutUIs];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    //退出页面需要销毁否则循环引用
    [self.wkWebView.configuration.userContentController removeScriptMessageHandlerForName:@"iosClientApi"];

}

- (void)layoutUIs {
    
    [self.view addSubview:self.wkWebView];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.urlString]];
    
    [self.wkWebView loadRequest:request];

    [self initRirghtBtns];

}

//通过接收JS传出消息的name进行捕捉的回调方法
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
        
    //用message.body获得JS传出的参数体
    if ([message.name isEqualToString:@"iosClientApi"]) {
        
        NSString *msgId = [message.body valueForKey:@"msgId"];
        NSString *method = [message.body valueForKey:@"method"];
        NSDictionary *param = [message.body valueForKey:@"param"];

        DDLog(@"----WKUserContentController----msgId:%@", msgId);
        DDLog(@"----WKUserContentController----method:%@", method);
        DDLog(@"----WKUserContentController----param:%@", param);

        [self evaluateJsMethod];
    }
    
}

// 调用js的方法
- (void)evaluateJsMethod {
  
    NSData *data = [NSJSONSerialization dataWithJSONObject:@{@"hid":kAppDelegate.curUser.hid, @"phone":kAppDelegate.curUser.phone, @"name":kAppDelegate.curUser.name} options:NSJSONWritingPrettyPrinted error:nil];

    NSString *jsonStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
     jsonStr = [jsonStr stringByReplacingOccurrencesOfString:@"\n" withString:@""];

    //   M.echo.push("m1", {a:1, b:3})
    NSString *js = [NSString stringWithFormat:@"M.echo.push('%@');",jsonStr];
//    NSString *js = [NSString stringWithFormat:@"M.echo.push(\"m1\", {a:1, b:3})"];

    dispatch_async(dispatch_get_main_queue(), ^{
        //执行的js代码
        [self.wkWebView evaluateJavaScript:js completionHandler:^(id _Nullable data, NSError * _Nullable error) {
            DDLog(@"1111111");
        }];
    });
    
}


//MARK: - lazy loading
- (WKWebView *)wkWebView {
    
    if (!_wkWebView) {
        
        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
        WKUserContentController *userContentController = [[WKUserContentController alloc] init];
        [userContentController addScriptMessageHandler:self name:@"iosClientApi"];
        configuration.userContentController = userContentController;

        _wkWebView = [[WKWebView alloc]initWithFrame:CGRectMake(0, -kStatusBarHeight-4, kScreenWidth, kScreenHeight) configuration:configuration];
        _wkWebView.UIDelegate = self;
        // 是否允许手势左滑返回上一级, 类似导航控制的左滑返回
        _wkWebView.allowsBackForwardNavigationGestures = YES;

    }
    return _wkWebView;
}
上一篇 下一篇

猜你喜欢

热点阅读