最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript interface with iPhone webview - Stack Overflow

programmeradmin2浏览0评论

I am converting coding from Android to iPhone, below is the code listen to "jb" from javascript in Android webview. How can I implement the code in iPhone?

  webView.loadUrl(url); 
    webView.setWebViewClient(new AppWebViewClient ());
    webView.addJavascriptInterface(new JavascriptBridge(), "jb");


final class JavascriptBridge
    {
        public void callback(String param){

            //Generate the returnValue from the bridge
            /*
            String toastValue = param
            Toast toast = Toast.makeText(AppHelp.this, toastValue, Toast.LENGTH_LONG);
            toast.show();
            */
            Log.i(TAG, param);
            if (param.equals("close")) {
                AppHelp.this.finish();
            }

        }
    }

I am converting coding from Android to iPhone, below is the code listen to "jb" from javascript in Android webview. How can I implement the code in iPhone?

  webView.loadUrl(url); 
    webView.setWebViewClient(new AppWebViewClient ());
    webView.addJavascriptInterface(new JavascriptBridge(), "jb");


final class JavascriptBridge
    {
        public void callback(String param){

            //Generate the returnValue from the bridge
            /*
            String toastValue = param
            Toast toast = Toast.makeText(AppHelp.this, toastValue, Toast.LENGTH_LONG);
            toast.show();
            */
            Log.i(TAG, param);
            if (param.equals("close")) {
                AppHelp.this.finish();
            }

        }
    }
Share Improve this question edited May 28, 2011 at 8:57 Ibu 43.9k14 gold badges82 silver badges109 bronze badges asked May 28, 2011 at 8:42 ErnestErnest 611 silver badge2 bronze badges 1
  • Did you find any way to achieve in iOS? Alternative to JavascriptInterface in iOS? – harshit2811 Commented Jul 22, 2015 at 9:51
Add a ment  | 

1 Answer 1

Reset to default 4

If I am reading this right, you want to convert this Android WebView code to iPhone. First, you create a UIWebView:

UIWebView* web = [[[UIWebView alloc] init] initWithURL:@"google."];

Second, you will want evaluate some JavaScript using the following method:

[web stringByEvaluatingJavaScriptFromString:@"JAVASCRIPT GOES HERE"];

To respond to a JavaScript function will be a little bit more difficult. I don't believe there is an iOS alternative to JavascriptBridge. You will want to see this question for further reference: Can I pass a variable from a UIWebView back to my app using Javascript (or any web technology)?

发布评论

评论列表(0)

  1. 暂无评论