The previous article briefly introduced the communication between iPhone applications, mainly through adding the URL scheme to Info. plist of the called application, and implementing Program Calling through openUrl in the application. Data transmission between applications can be implemented with a more url. For example, I want to input a parameter in the test application and pass it to URLSchemeDemo. You can achieve this by using the following method:
NSString *temp = [NSString stringWithFormat:@"URLSchemeDemoapp:message=%@",self.message.text];
NSURL *url = [NSURL URLWithString:temp];
[[UIApplication sharedApplication] openURL:url];
Message. text is the input data, and URLSchemeDemoapp is the URL scheme of the called program URLSchemeDemo.
Next, how does URLSchemeDemo accept this parameter? In UIApplicationDelegate
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) in the launchOptions method, the parameter dictionary launchOptions contains the name of the application that calls the function (response) and URL (UIApplicationLaunchOptionsURLKey ). This method is implemented as follows:
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
{
Self. window = [[[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds] autorelease];
// Override point for customization after application launch.
Self. viewController = [[ViewController alloc] initWithNibName: @ "ViewController" bundle: nil] autorelease];
Self. window. rootViewController = self. viewController;
[Self. window makeKeyAndVisible];
// Process information sharing between applications
If (launchOptions ){
NSString * sourceApp = [launchOptions objectForKey: UIApplicationLaunchOptionsSourceApplicationKey];
NSURL * url = [launchOptions objectForKey: UIApplicationLaunchOptionsURLKey];
NSString * msg = [NSString stringWithFormat: @ "sourceApp: % @, url: % @", sourceApp, url];
Self. viewController. message. text = msg;
}
Return YES;
}
First, determine whether it is called. If it is called, the caller's identifier and the called URL are output.
The program effect is as follows:
Test Application
URLSchemeDemo Application
Enter the parameter "123" in "test" and click "call.
The name and path are successfully transferred.
However, when didfinishlaunchingwitexceptions is used to obtain the url, the program can only call one second call without changing the input information. This is because didfinishlaunchingwitexceptions is only called when the program is running. Use the-(BOOL) application of UIApplicationDelegate :( UIApplication *) application handleOpenURL :( NSURL *) url to solve this problem, this function is called every time another application uses a url to call this application. The parameter is url. Therefore, it is more convenient to call this function.
/*
* Response method when other applications call the program using the URL Scheme
*/
-(BOOL) application :( UIApplication *) application handleOpenURL :( NSURL *) url {
Self. viewController. message. text = [NSString stringWithFormat: @ "% @", url];
Return YES;
}