JavaScript与 OC 的交互使用

如果没有接触过 JavaScript 的可以在这里看看JavaScript菜鸟教程,我也是在上面学习的JavaScript
在这里以WKWebView为主。

第一步,创建WKWebView并配置:

  • 导入头文件
    #import <WebKit/WebKit.h>
    #import <JavaScriptCore/JavaScriptCore.h>
  • delegate

    <WKUIDelegate,WKNavigationDelegate,WKScriptMessageHandler>
  • WKWebView 对象
- (WKWebView *)wkWebview
{
    if (_wkWebview == nil)
    {
        
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
        //TODO:设置偏好设置
        config.preferences = [[WKPreferences alloc] init];
        //1-默认为0
        config.preferences.minimumFontSize = 80;
        //2-JavaScript 是否可用,默认认为YES
        config.preferences.javaScriptEnabled = YES;
        //3-在iOS上默认为NO,表示不能自动通过窗口打开
        config.preferences.javaScriptCanOpenWindowsAutomatically = YES;
        config.processPool = [[WKProcessPool alloc] init];
        config.userContentController = [[WKUserContentController alloc] init];
        

    

        _wkWebview = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT==812?(HEIGHT - 34):HEIGHT)
                                       configuration:config];
        _wkWebview.UIDelegate = self;
        _wkWebview.navigationDelegate = self;
        _wkWebview.backgroundColor = [UIColor clearColor];
        _wkWebview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _wkWebview.multipleTouchEnabled = YES;
        _wkWebview.autoresizesSubviews = YES;
        _wkWebview.scrollView.alwaysBounceVertical = YES;
        _wkWebview.allowsBackForwardNavigationGestures = YES;/**这一步是,开启侧滑返回上一历史界面**/
        [self.view addSubview:_wkWebview];
    }
    return _wkWebview;
}

第二步、监听网页上的弹窗事件并回传信息

JavaScript弹窗分为三种,alert警告框confirm确认框prompt提示窗。在 Objective-C中分别有对应的方法来接收和响应他们的操作并且回传需要的信息。

alert警告框
#pragma mark 接收网页端过来的 alert 信息
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:confirm];
    [self presentViewController:alert animated:YES completion:nil];
    completionHandler();
}
confirm确认框
#pragma mark 接收确认框信息,并提交操作
- (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler{    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        completionHandler(YES);
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        completionHandler(NO);
    }];
    
    [alert addAction:confirm];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}
prompt提示窗
#pragma mark 接收网页端 prompt 信息,并提交输入内容
- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable result))completionHandler{    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:prompt preferredStyle:UIAlertControllerStyleAlert];
    [alert  addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        
    }];
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        completionHandler(alert.textFields[0].text);
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        completionHandler(@"");
    }];
    
    [alert addAction:confirm];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

第三步、响应其它网页端事件,接收JavaScript传递过来的值。

如下,html 中有一个 button 元素,它响应的JavaScript函数为eoc()

<button id="btn4" style="width:80%;height: 88px;padding: inherit" type="button" onClick="eoc()">
    Action01    
</button>

<script>
  function eoc(){
  }
</script>

如何在OC端响应这个消息呢?

  • 我们可以采取类似OC中观察者模式的方式,在eoc()中添加 window.webkit.messageHandlers.htmlAction01.postMessage('点击了第一个按钮');来向OC发送一条信息,具体的格式为:
    window.webkit.messageHandlers.ActionObjectName.postMessage('所需传递的值或信息');
  • OC中通过WKWebView添加消息监听,具体为
//4-在这里设置监听代理,监听 JavaScript 对象的发出的信息
[config.userContentController addScriptMessageHandler:self name:@"htmlAction01"];
[config.userContentController addScriptMessageHandler:self name:@"htmlAction02"];
[config.userContentController addScriptMessageHandler:self name:@"htmlAction03"];
  • 在代理方法中做出操作
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
    if ([message.name isEqualToString:@"htmlAction01"]) {
        NSLog(@"*01***%@***",message);
    }else if ([message.name isEqualToString:@"htmlAction02"]){
        NSLog(@"*02***%@***",message);
    }else if ([message.name isEqualToString:@"htmlAction03"]){
        NSLog(@"*03***%@***",message);
    }
        [self alertWithMessage:message.body];
}

第四步、JavaScript响应OC的消息,接收传递过来的值

  • 添加新元素节点
- (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ _Nullable)(_Nullable id, NSError * _Nullable error))completionHandler;

具体的使用:

NSString *jsValue = @"var ne = document.createElement('button');
                      var nc = document.createTextNode('后来添加的button');
                      ne.addEventListener('click',function(){alert('这是新添加 btn 的测试弹窗')});
                      ne.appendChild(nc);
                      document.getElementById('bby').appendChild(ne);"
[webView evaluateJavaScript:jsValue completionHandler:^(id _Nullable response, NSError * _Nullable error) {
        NSLog(@"*001***** %@ *****error:%@",response,error);
    }];

上面的操作会找到idbby的元素,并向这个元素节点之中添加一个button元素,同时这个button元素会响应function(){alert('这是新添加 btn 的测试弹窗')这个操作;

  • 传值给JavaScript

方式一:
在网页端有这样一个函数

function userLogin(isLogin){
    alert(isLogin?"用户已登录":"用户未登录");
}

OC端的操作

 NSString *jsValue = [NSString stringWithFormat:@"userLogin(%d)",YES];
 [self.wkWebview evaluateJavaScript: jsValue completionHandler:^(id _Nullable response, NSError * _Nullable error) {
     NSLog(@"*002***** %@ *****error:%@",response,error);
}];

  • 方式二
NSString *jsValue = @"confirm('{name:Tom,age:18,height:188}');";
 [self.wkWebview evaluateJavaScript: jsValue completionHandler:^(id _Nullable response, NSError * _Nullable error) {
    NSLog(@"*003***** %@ *****error:%@",response,error);
}];

不足的地方,还请多多指教,谢谢了。



更新:经过一位大神同学指导,
关于JavaScript调用 原生方法
因为window.webkit.messageHandlers.ActionObjectName.postMessage('所需传递的值或信息');只在第一次注册时有效,且可能会随着代码量的增加要注册很多个,这样就麻烦一些了。
所以,当我们想通过JavaScript调用 原生方法且需要回传值时,可以在JavaScript端对prompt的参数进行封装(大神同学采取的是对prompt封装,我暂时没有看懂他写的,就对prompt的参数进行了封装),这样就能在不注册window.webkit.messageHandlers.ActionObjectName.postMessage('所需传递的值或信息');的时候随时调用 原生方法。
思路:

JavaScript

function bridge(nativeMethod,otherParams,isNativeMethod,promptStr){
        var txt = {
            'nativeMethod':nativeMethod,
            'otherParams':otherParams,
            'isNativeMethod':isNativeMethod,
            'promptStr':promptStr
        };
        console.log(JSON.stringify(txt));
        return prompt(JSON.stringify(txt),'');
    }

Objective-C
在原生端,可以根据isNativeMethod参数来判断是调用原生方法,还是弹起弹窗。
nativeMethod参数是所要调用原生方法的方法名字符串,通过NSSelectorFromString(<#NSString * _Nonnull aSelectorName#>)转为为原生方法。
otherParams是其它需要传递的参数。
promptStr是弹窗的提示信息。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,366评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,521评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,689评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,925评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,942评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,727评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,447评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,349评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,820评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,990评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,127评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,812评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,471评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,017评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,142评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,388评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,066评论 2 355

推荐阅读更多精彩内容