zoukankan      html  css  js  c++  java
  • 转:EasyJSWebView

    EasyJSWebView 是类似 Android  javascriptInterface 的 uiwebview js 调用原生代码框架

    示例代码:

    先建一个MyJSInterface接口

    @interface MyJSInterface : NSObject - (void) test;
    - (void) testWithParam: (NSString*) param;
    - (void) testWithTwoParam: (NSString*) param AndParam2: (NSString*) param2;
    - (NSString*) testWithRet;
     @end

    然后把MyJSInterface 添加到 UIWebView.

    MyJSInterface* interface = [MyJSInterface new];
    [self.myWebView addJavascriptInterfaces:interface WithName:@"MyJSTest"];
    [interface release];

    在Javascript中, 你可以使用如下代码调用 Objective-C 方法.

    MyJSTest.test();
    MyJSTest.testWithParam("ha:ha");
    MyJSTest.testWithTwoParamAndParam2("haha1", "haha2"); 
    var str = MyJSTest.testWithRet();

    你也可以通过回调获取到方法执行结果

    Objective-C 代码如下:

    - (void) testWithFuncParam: (EasyJSDataFunction*) param{ 
      NSLog(@"test with func"); 
      NSString* ret = [param executeWithParam:@"blabla:"bla"]; 
      NSLog(@"Return value from callback: %@", ret);
    }

    js调用方法如下:

    MyJSTest.testWithFuncParam(function (data){ 
      alert(data); //data would be blabla:"bla return "some data";
    });
  • 相关阅读:
    开源ITIL管理软件iTop 2.5-2.6安装
    并发服务器
    套接字通信
    libevent
    gdb调试
    值得收藏的技术社区
    关于博客园随笔编辑页面内容不刷新(空白)的问题解决
    嵌入式Web框架
    内存地址的传递问题
    linux文件缓冲区
  • 原文地址:https://www.cnblogs.com/guoxiaoqian/p/4784276.html
Copyright © 2011-2022 走看看