简体   繁体   English

iOS:在UIWebview中使用javascript调用obj-c方法

[英]iOS: call obj-c methods using javascript in a UIWebview

I am writing a function that collaborates with a JS web page. 我正在编写一个与JS网页协作的函数。 I use UIWebView to contain the webpage and then situation has become complicated when I want the web page to communicate with my app. 我使用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? 但是有没有更简单的方法来调用网页中的obj-c函数,使用javascript? I tried using the UIWebView delegate method, but I think it's too hacky. 我尝试使用UIWebView委托方法,但我认为它太hacky了。

Any advice? 有什么建议?

I guess using delegate is the only (one or two) methodology you can use in iOS WebView. 我想使用委托是您可以在iOS WebView中使用的唯一(一个或两个)方法。 But there are several wrappers that may help you easy out. 但有几个包装可以帮助你轻松。

  1. EasyJSWebView - This replicates the development experience as in Android. EasyJSWebView - 这复制了Android中的开发体验。 In Android, you can simply use the addJavascriptInterface() method in WebView to bridge the Javascript to Java. 在Android中,您可以简单地使用WebView中的addJavascriptInterface()方法将Javascript桥接到Java。 EasyJSWebView provides both sync-style and async-style for getting the return value from Objective-C methods. EasyJSWebView提供同步样式和异步样式,用于从Objective-C方法获取返回值。

  2. WebViewJavascriptBridge - The code may look a little bit like socket programming. WebViewJavascriptBridge - 代码看起来有点像套接字编程。 You can pass data to and fro between the "server" in Objective-C and the "client" in Javascript. 您可以在Objective-C中的“server”和Javascript中的“client”之间来回传递数据。

  3. GAJavaScript - This may provide a better DOM manipulation experience. GAJavaScript - 这可以提供更好的DOM操作体验。

Take a look at all of them and choose one that fits your need. 看一下所有这些并选择一个符合您需求的产品。

Yes it does feel hacky and is a little laggy but you need to do it with the UIWebViewDelegate 是的,它确实感觉hacky并且有点迟钝但你需要使用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;
}

如果您正在考虑升级到WKWebView, XWebView可能是最好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM