Objective C 从另一个应用程序检索数据
我编写了两个应用程序,我需要它们之间的交互。我的意思是,在第一个应用程序中,有一个视图,您需要在其中添加一个数字。但如果您不想写下来,另一个应用程序会为您生成它。所以我需要第二个应用程序生成的数字,并用它填充视图。
我读了很多关于这个主题的文章,我知道我必须使用自定义 URL 方案。例如,关于此的一篇好文章:链接 。所以如果我理解的话,我需要像这样定义方案:
在第一个应用程序中:
- URL标识符: com.mycompany.myfirstapp
- URL 方案第一项:myFirstApp
在第二个应用程序中:
- URL 标识符: com.mycompany.mysecondapp
- URL 方案第一项: mySecondApp
然后在第一个应用程序中,例如在按钮的 IBAction 中:
[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mySecondApp://pleaseGenerateNumber"]];
在第二个应用程序中,我必须实现
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法和
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
方法。然后解析URL,并生成数字。但问题就在这里。我如何“告诉”第一个应用程序第一个应用程序生成的号码?我必须再次使用openURL,并通过URL检索号码吗?
I wrote two applications, and I need interaction between them. I mean, in the first application, there is a view, where you need to add a number. But if you don't want to write it down, the other app generate it for you. So I need the number, which the second app generates, and fill the view with it.
I read a lot of about this theme, I know I have to use custom URL schemes. A good article about this for example: link. So if I understand, I need to define schemes like this:
In the first app:
- URL identifier:
com.mycompany.myfirstapp - URL Schemes first item: myFirstApp
In the second app:
- URL identifier:
com.mycompany.mysecondapp - URL Schemes first item: mySecondApp
Then in the first app, for example in a button's IBAction:
[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mySecondApp://pleaseGenerateNumber"]];
In the second app, I have to implement the
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
method and the
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
method. Then parse the URL, and do the number generating. But the problem is here. How can I "tell" to the first app the number generated by the first app? I have to use again openURL, and retrieve the number via URL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 iOS4.2 中可用,要使用某种方案将数据从一个应用程序传递到另一个应用程序,您可以使用 UIApplicationDelegate 协议方法
从文档中,注释是:
编辑:事实证明,您可以在 iOS 3.2 下使用注释;
application:DidFinishLaunchingWithOptions:
允许在选项字典中使用注释键。Available in iOS4.2, to pass data from one application to another using a scheme you can use the
UIApplicationDelegate
protocol methodFrom the docs,
annotation
is:EDIT: It turns out that you can use an annotation under iOS 3.2;
application:DidFinishLaunchingWithOptions:
allows for an annotation key in the options dictionary.为了获得最大的兼容性(iOS 3+),是的,您必须再次使用 openURL,将数字作为 URL 参数传回。要重用系统提供的 URL 解析代码,请确保您的 URL 格式遵循 HTTP 模板:
schema://kinda-host/kinda-path?params
考虑使用 kinda-host 作为命令代码并将数据作为路径和/或传递参数;这样您就可以扩展您的通信协议,而无需引入额外的 URL 方案。
是的,这很丑。 iOS 就是这样。
For maximum compatibility (iOS 3+), yes, you have to use openURL again, passing the number back as a URL parameter. To reuse the system-provided URL parsing code, make sure your URL format follows the HTTP template:
schema://kinda-host/kinda-path?params
Consider using kinda-host as a command code and pass data as path and/or parameters; this way you can extend your communication protocol without introducing extra URL schemes.
Yes, this is ugly. Such is iOS.