Data transmission between iPhone applications

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.