首页 > Web开发 > 详细

转:EasyJSWebView

时间:2015-09-06 01:02:53      阅读:346      评论:0      收藏:0      [点我收藏+]

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

 

转:EasyJSWebView

原文:http://www.cnblogs.com/guoxiaoqian/p/4784276.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!