EasyJSWebViewJS 原生代碼框架
EasyJSWebView 是類似 Android javascriptInterface 的 uiwebview js 調(diào)用原生代碼框架。
示例代碼:
先建一個(gè)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中, 你可以使用如下代碼調(diào)用 Objective-C 方法.
MyJSTest.test();
MyJSTest.testWithParam("ha:ha");
MyJSTest.testWithTwoParamAndParam2("haha1", "haha2"); var str = MyJSTest.testWithRet();
你也可以通過回調(diào)獲取到方法執(zhí)行結(jié)果
Objective-C 代碼如下:
- (void) testWithFuncParam: (EasyJSDataFunction*) param{ NSLog(@"test with func"); NSString* ret = [param executeWithParam:@"blabla:\"bla"]; NSLog(@"Return value from callback: %@", ret);
}
js調(diào)用方法如下:
MyJSTest.testWithFuncParam(function (data){ alert(data); //data would be blabla:"bla return "some data";
});
更多使用方法點(diǎn)擊這里查看案例工程。
評(píng)論
圖片
表情
