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
1 Answer
Reset to default 4If 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)?