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 implem开发者_开发知识库ent 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();
}
}
}
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)?
精彩评论