开发者

Objective C retrieve data from another application

开发者 https://www.devze.com 2023-02-24 10:13 出处:网络
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

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 ag开发者_开发技巧ain openURL, and retrieve the number via URL?


Available in iOS4.2, to pass data from one application to another using a scheme you can use the UIApplicationDelegate protocol method

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url 
       sourceApplication:(NSString *)sourceApplication 
       annotation:(id)annotation

From the docs, annotation is:

A property-list object supplied by the source application to communicate information to the receiving application.

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消