xcode iOS:在 UIWebview 中使用 javascript 调用 obj-c 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14454935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS: call obj-c methods using javascript in a UIWebview
提问by dreamy129
I am writing a function that collaborates with a JS web page. I use UIWebView to contain the webpage and then situation has become complicated when I want the web page to communicate with my app.
我正在编写一个与 JS 网页协作的函数。我使用 UIWebView 来包含网页,然后当我希望网页与我的应用程序通信时情况变得复杂。
Calling a javascript function in UIWebView is easy by using the – stringByEvaluatingJavaScriptFromString:
method
使用– stringByEvaluatingJavaScriptFromString:
方法在 UIWebView 中调用 javascript 函数很容易
But is there any easier way to call an obj-c function in the web page, using javascript? I tried using the UIWebView delegate method, but I think it's too hacky.
但是有没有更简单的方法来使用 javascript 在网页中调用 obj-c 函数?我尝试使用 UIWebView 委托方法,但我认为它太hacky了。
Any advice?
有什么建议吗?
回答by Dukeland
I guess using delegate is the only (one or two) methodology you can use in iOS WebView. But there are several wrappers that may help you easy out.
我想使用委托是您可以在 iOS WebView 中使用的唯一(一种或两种)方法。但是有几个包装器可以帮助您轻松完成。
EasyJSWebView- This replicates the development experience as in Android. In Android, you can simply use the addJavascriptInterface() method in WebView to bridge the Javascript to Java. EasyJSWebView provides both sync-style and async-style for getting the return value from Objective-C methods.
WebViewJavascriptBridge- The code may look a little bit like socket programming. You can pass data to and fro between the "server" in Objective-C and the "client" in Javascript.
GAJavaScript- This may provide a better DOM manipulation experience.
EasyJSWebView- 这复制了 Android 中的开发体验。在 Android 中,您可以简单地使用 WebView 中的 addJavascriptInterface() 方法将 Javascript 桥接到 Java。EasyJSWebView 提供了同步样式和异步样式来从 Objective-C 方法中获取返回值。
WebViewJavascriptBridge- 代码看起来有点像套接字编程。您可以在 Objective-C 中的“服务器”和 Javascript 中的“客户端”之间来回传递数据。
GAJavaScript- 这可能会提供更好的 DOM 操作体验。
Take a look at all of them and choose one that fits your need.
看看所有这些,然后选择一个适合您的需求。
回答by robhayward
Yes it does feel hacky and is a little laggy but you need to do it with the UIWebViewDelegate
是的,它确实感觉很笨拙,而且有点滞后,但您需要使用 UIWebViewDelegate 来做到这一点
function init()
{
$('input').on('click', function(e) { answerBoxShouldBeginEditing(e); });
}
function answerBoxShouldBeginEditing(e)
{
var object = e.toElement;
var answer = $(object).attr('name');
var request = 'engine:' + answer;
var iframe = document.createElement('IFRAME');
iframe.setAttribute('src', request);
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *requestString = [[request URL] absoluteString];
if ([requestString hasPrefix:@"engine:"]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
return NO;
}
return YES;
}