转: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";
});

 

posted @ 2015-09-06 00:13  郭晓倩  阅读(625)  评论(0编辑  收藏  举报