jiaojinggang1751 2016-01-12 01:14 采纳率: 0%
浏览 1874
已结题

用JavaScriptCore js调用OC

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>失败</title>
    <script type="text/javascript">
        function redirectNative() {
            bluewhale.funTo("FAIL");
        }
    </script>
    <style>
        .msg {
            width: 100%;
            margin-top: 30%;
            font-size: 30px;
            font-weight: lighter;
            text-align: center;
        }

        .btn {
            margin-top: 60%;
            background-color: #1478f0;
            position: relative;
            height: 45px;
            bottom: 20%;
            left: 10%;
            width: 80%;
            font-size: 20px;
            color: white;
            font-weight: lighter;
            border-radius: 5px;
        }
    </style>
</head>
<body>
<div>
    <div class="msg">${message}</div>
</div>
<div><img src=""/></div>
<button class="btn" onclick="redirectNative()">返回</button>
</body>
</html>
  • 写回答

2条回答 默认 最新

  • yuanwang886 2016-01-12 07:56
    关注
    1. 如果你是要调用redirectNative(),哪么简单了
      在 - (void)webViewDidFinishLoad:(UIWebView *)webView方法里:
      JSContext *jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

      jsContext[@"redirectNative"] = ^() {
      dispatch_async(dispatch_get_main_queue(), ^{
      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"js_redirectNative调用OC" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
      [alertView show];
      });
      };

    2. 如果你是想实现 bluewhale.funTo("FA1IL"); 调用OC,哪么就稍微复杂点

      (1)协议
      @protocol TestProtocol

                  - (void)funTo:(NSString *)some; //这个方法与你js方法同名,同个数的参数
      
          @end
      
      (2) 
      

      @interface TestModel()

          @end
      
          @implementation TestModel
      
          - (void)funTo:(NSString *)some
          {
      

      dispatch_async(dispatch_get_main_queue(), ^{
      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:some delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
      [alertView show];
      });
      }
      @end

      (3) 还是在- (void)webViewDidFinishLoad:(UIWebView *)webView方法里:
      
          JSContext *jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
      
          //如果是funTo调用
          TestModel *tm = [TestModel new];
          jsContext[@"bluewhale"] = tm;
      

      你可以根据这些自己在整理封装下

    评论

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败