Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript interface with iPhone webview

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

        }
    }
like image 438
Ernest Avatar asked May 28 '11 08:05

Ernest


1 Answers

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.com"];

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)?

like image 174
Chris Truman Avatar answered Nov 18 '22 03:11

Chris Truman