iOS html页面交互传值

2019-12-05  本文已影响0人  前尘梦醒

1.通过JS向APP原生传值

- (void)webViewDidFinishLoad:(UIWebView*)webView

{

    CGFloat sizeHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"]floatValue];

    self.webView.frame = CGRectMake(0,0,SCREEN_WIDTH, SCREEN_HEIGHT);

    // 获取JS的上下文

    JSContext*context = [webView  valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

//jumpShowImagesActivity 与服务器约定好的方法名

    context[@"jumpShowImagesActivity"] = ^() {

        // 获取到JSContext中的参数

        NSArray *args = [JSContext currentArguments];

        NSString*imageUrl;

        NSArray*imageArr;

        for(int i = 0; i<args.count; i++){

            JSValue *jsVal = args[i];

            if(i==0) {

                imageUrl = [jsVal toString];

            }else{

                imageArr = [jsVal toArray];

            }

        }

        NSInteger index = [imageArr indexOfObject:imageUrl];

        dispatch_async(dispatch_get_main_queue(), ^{

            [[XLImageViewer shareInstanse] showNetImages:imageArr index:index fromImageContainer:self.view title:@[]];

        });

        JSValue *content = [JSContext currentThis];

        NSLog(@"content为: %@",content);

    };

    context.exceptionHandler= ^(JSContext*context,JSValue*exceptionValue) {

        context.exception= exceptionValue;

        // 比如把js中的方法名改掉,OC找不到相应方法,这里就会打印异常信息

        NSLog(@"异常信息:%@", exceptionValue);

    };

}

2.APP向html传值 (wkwebview)

    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];

        configuration.userContentController = [WKUserContentController new];

        [configuration.userContentController addScriptMessageHandler:self name:@"jumpLogin"];

        [configuration.userContentController addScriptMessageHandler:self name:@"getUserName"];

        [configuration.userContentController addScriptMessageHandler:self name:@"jumpUserInfoPage"];

        WKPreferences *preferences = [WKPreferences new];

        preferences.javaScriptCanOpenWindowsAutomatically = YES;

        preferences.javaScriptEnabled=YES;

        configuration.preferences= preferences;

        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) configuration:configuration];

        _webView.UIDelegate=self;

        _webView.navigationDelegate = self;

需要服务器先行注册一个方法给APP调用,然后在代理方法中实现传值

- (void)userContentController:(WKUserContentController*)userContentController didReceiveScriptMessage:(WKScriptMessage*)message

{

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    NSString*userName = [userDefaultsobjectForKey:@"KEY_USER_NAME"];

    [UsergetAccount:^(NSString*username,NSString*password,NSString*token) {

    NSLog(@"username:%@,password:%@,token:%@",username,password,token);

     NSString* jsStr = [NSStringstringWithFormat:@"getUserName('%@,%@')",userName,password];               [self.webViewevaluateJavaScript:jsStrcompletionHandler:^(id_Nullableresult,NSError*_Nullableerror) {

                    NSLog(@"result=%@  error=%@",result, error);

                }];

            }];

}

上一篇下一篇

猜你喜欢

热点阅读