JavascriptWebViewCore.framework的使用DEMO

案例


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界面

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容