如何从JavaScript调用Objective-C? [英] How to call Objective-C from Javascript?

查看:85
本文介绍了如何从JavaScript调用Objective-C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WebView,我想从JavaScript调用一个视图在Objective-C。有人知道我该如何做吗?






我在ViewController中有这个代码:

   - (BOOL)webView:(UIWebView *)webView2 
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {

NSString * requestString = [[request URL] absoluteString];
NSArray * components = [requestString componentsSeparatedByString:@:];

if([components count]> 1&
[(NSString *)[components objectAtIndex:0] isEqualToString:@myapp]){
if ([NSString *] [components objectAtIndex:1] isEqualToString:@myfunction])
{

NSLog([components objectAtIndex:2]); [[飞艇共享] displayStoreFront]; //< - 这是打开商店的代码
NSLog([components objectAtIndex:3]); // param2
//使用上面的命令在Objective-C方法中调用你的方法...
}
return NO;
}

return YES; // Return YES以确保常规导航按预期工作。
}

在Javascript中:

  function store(event)
{
document.location =myapp:+myfunction:+ param1 + :+ param2;
}

但没有任何反应。

UIWebView 的标准解决方法是设置 UIWebViewDelegate ,并实施方法 webView:shouldStartLoadWithRequest:navigationType: 。在您的JavaScript代码中,导航到一个假的网址,其中包含您要传递给应用的信息,例如:

  window.location =fake:// myApp / something_happened:param1:param2:param3; 

在您的委托方法中,查找这些假网址,提取您需要的信息,适当,并返回 NO 以取消导航。如果您使用某些风味 performSelector


I have a WebView, and I want to call a view in Objective-C from JavaScript. Does someone know how I can do this?


I have this code in my ViewController:

- (BOOL)webView:(UIWebView *)webView2 
 shouldStartLoadWithRequest:(NSURLRequest *)request 
 navigationType:(UIWebViewNavigationType)navigationType {

 NSString *requestString = [[request URL] absoluteString];
 NSArray *components = [requestString componentsSeparatedByString:@":"];

 if ([components count] > 1 && 
  [(NSString *)[components objectAtIndex:0] isEqualToString:@"myapp"]) {
  if([(NSString *)[components objectAtIndex:1] isEqualToString:@"myfunction"]) 
  {

   NSLog([components objectAtIndex:2]); [[Airship shared] displayStoreFront]; //<- This is the code to open the Store
   NSLog([components objectAtIndex:3]); // param2
   // Call your method in Objective-C method using the above...
  }
  return NO;
 }

 return YES; // Return YES to make sure regular navigation works as expected.
}

And in Javascript:

function store(event)
{
    document.location = "myapp:" + "myfunction:" + param1 + ":" + param2;
}

But nothing happens.

解决方案

The standard workaround for UIWebView is to set a UIWebViewDelegate, and implement the method webView:shouldStartLoadWithRequest:navigationType:. In your JavaScript code, navigate to some fake URL that encodes the information you want to pass to your app, like, say:

window.location = "fake://myApp/something_happened:param1:param2:param3";

In your delegate method, look for these fake URLs, extract the information you need, take whatever action is appropriate, and return NO to cancel the navigation. It's probably best if you defer any lengthy processing using some flavor of performSelector.

这篇关于如何从JavaScript调用Objective-C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆