简体   繁体   English

javascript与Objective-c(IOS)之间的通信

[英]Communication between javascript and Objective-c (IOS)

I know the way to communicate between iOS and javascript is to create fake URLs in JS and append a string which will be used as parameter to the Objective-C code and parse that URL in this delegate method: 我知道在iOS和javascript之间进行通信的方法是在JS中创建伪造的URL并附加一个字符串,该字符串将用作Objective-C代码的参数,并在此委托方法中解析该URL:

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

Then send the return value by calling the native method stringByEvaluatingJavaScriptFromString again with parameter callID. 然后,通过再次使用参数callID调用本机方法stringByEvaluatingJavaScriptFromString发送返回值。

But I want to, just like Android, directly call Objective-C methods from JS and get a return value. 但是我想像Android一样,直接从JS调用Objective-C方法并获取返回值。 Instead of needing to write any method in JavaSCript to get the return value. 无需在JavaSCript中编写任何方法来获取返回值。 Actually we have JS code already. 实际上我们已经有了JS代码。 It works perfectly while communicating with Android. 与Android通信时,它可以完美运行。

Are there any tricks to solve my problem that will be approved on the AppStore? 是否有任何技巧可以解决我的问题,这些问题将在AppStore上得到批准?

There is no built-in method to do this, however you can use a library such as WebViewJavascriptBridge: https://github.com/marcuswestin/WebViewJavascriptBridge . 没有内置方法可以执行此操作,但是您可以使用WebViewJavascriptBridge之类的库: https : //github.com/marcuswestin/WebViewJavascriptBridge

Even if there were a built-in solution, there are no standards for this. 即使有内置的解决方案,对此也没有标准。 As such it wouldn't work the same way as it does on Android either way. 因此,它与Android上的任何一种方式都不一样。 Communicating with Obj-C code means that you're moving away from the browser and on to platform specific implementations which, by nature, vary between platforms. 与Obj-C代码进行通信意味着您正在离开浏览器,转而使用平台特定的实现,这些实现在不同平台之间自然有所不同。

You can call javascript methods from objective C using simple steps. 您可以使用简单的步骤从目标C调用javascript方法。

Please go through this also see the Apple Documentation 请仔细阅读此内容,另请参阅Apple文档

If i understood your question properly that you want to invoke Objective-c method from Java script then below is the example which i have referred from Apple Documentation :- 如果我正确理解了您想从Java脚本调用Objective-c方法的问题,那么下面是我从Apple文档中引用的示例:-

Let's look at a sample class. 让我们看一个示例类。 In this case, we will create an Objective-C address book class and expose it to JavaScript. 在这种情况下,我们将创建一个Objective-C通讯簿类,并将其公开给JavaScript。 Let's start with the class definition: 让我们从类定义开始:

@interface BasicAddressBook: NSObject {
}
+ (BasicAddressBook *)addressBook;
- (NSString *)nameAtIndex:(int)index;
@end

Now we'll write the code to publish a BasicAddressBook instance to JavaScript: 现在,我们将编写代码以将BasicAddressBook实例发布到JavaScript:

BasicAddressBook *littleBlackBook = [BasicAddressBook addressBook];

id win = [webView windowScriptObject];
[win setValue:littleBlackBook forKey:@"AddressBook"];

Once you expose these methods to JavaScript (described at the end of this section), you should be able to access your basic address book from the JavaScript environment and perform actions on it using standard JavaScript functions. 将这些方法暴露给JavaScript(在本节末尾介绍)后,您应该能够从JavaScript环境访问基本地址簿,并使用标准JavaScript函数对其执行操作。

Now, let's make an example showing how you can use the BasicAddressBook class instance in JavaScript. 现在,让我们做一个示例,展示如何在JavaScript中使用BasicAddressBook类实例。 In this case, we'll print the name of a person at a certain index in our address book: 在这种情况下,我们将在地址簿中的某个索引处打印人的名字:

function printNameAtIndex(index) {
    var myaddressbook = window.AddressBook;
    var name = myaddressbook.nameAtIndex_(index);
    document.write(name);
}

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

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