案例
1.前端网页调用客户端:
- 前端代码网页:
JS call iOS
ClientAPI.toReply = toReply;
//ClientAPI_callPay调用IOS方法,a b为参数。
ClientAPI.toReply('Hello','World');
- 客户端代码:
_content[@"toReply"] = ^{
NSLog(@"JS call iOS success");
//从JS返回的参数存储在args数组中
NSArray *args = [JSContext currentArguments];
for (JSValue* jsVal in args) {
NSLog(@"--%@",jsVal);
}
- 客户端控制台输出:
JS call iOS success
--Hello
--World
2.客户端调用前端网页:
- 前端代码:
iOS调用JS方法
var CommentApp ={
publishCommentCallback: function (userUid, userNickName, text) {
//TODO
},
functionCallBack:function(text){
alert(text);
}
};
-
客户端代码:
NSString* publishString = [NSString stringWithFormat:@"Comment.publishCommentCallback('%@','%@','%@')",[MTDGlobalObject shareGlobalObject].currentUser.userUid,userNickName,text]; [_commentWebView stringByEvaluatingJavaScriptFromString:publishString]; NSString* functionString = [NSString stringWithFormat:@"Comment.functionCallBack('%@')",text]; [_commentWebView stringByEvaluatingJavaScriptFromString:functionString];
前端网页输出:
跳出alert界面