最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

iphone - iOS: call obj-c methods using javascript in a UIWebview - Stack Overflow

programmeradmin4浏览0评论

I am writing a function that collaborates with a JS web page. I use UIWebView to contain the webpage and then situation has bee plicated when I want the web page to municate with my app.

Calling a javascript function in UIWebView is easy by using the – stringByEvaluatingJavaScriptFromString: method

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.

Any advice?

I am writing a function that collaborates with a JS web page. I use UIWebView to contain the webpage and then situation has bee plicated when I want the web page to municate with my app.

Calling a javascript function in UIWebView is easy by using the – stringByEvaluatingJavaScriptFromString: method

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.

Any advice?

Share Improve this question edited Oct 1, 2013 at 8:48 Master Stroke 5,1282 gold badges27 silver badges58 bronze badges asked Jan 22, 2013 at 9:09 dreamy129dreamy129 452 silver badges5 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

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.

  1. 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.

  2. 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.

  3. GAJavaScript - This may provide a better DOM manipulation experience.

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

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

If you are considering to upgrade to WKWebView, XWebView may be the best solution.

发布评论

评论列表(0)

  1. 暂无评论