IOS---Jump and pass data between apps via OpenURL

Source: Internet
Author: User

In iOS, implementing an app to launch another application, using UIApplication's OpenURL: Method can be implemented, here is the example of test jumping to test02. (These two projects need to be created first)

Registering a Custom URL protocol (in test)

The app that was launched first needs to register a custom URL protocol with the iphone. This is done in the Info.plist file.

1. Right-click and select "Add Row"
2. Key value Select "URL Types"
3. Open the Item 0″ and add a URL identifier for the key. Can be any value, but it is recommended to use "anti-domain name" (for example, "Com.fcplayer.test").
4. Add a line under "Item 0".
5. Select "URL schemes" as key.
6. Enter your URL protocol name (e.g. "test://" should be written "test"). If necessary, you can add multiple protocols here.
The operation is as follows:

  

Accessing a custom URL (in test02) launches another application in the main application by accessing the custom URL: (test is already installed, this code is written in another application, such as test02)

Put it where you need it, call it.
Nsurl * Urlstr = [Nsurl urlwithstring:@ "test://x=100"];//followed by parameters
if ([[[UIApplication sharedapplication] canopenurl:urlstr]) {
NSLog (@ "can go to test");
[[UIApplication sharedapplication] openurl:urlstr];
}else{
NSLog (@ "Can not go to test!!!!! ");
}

Custom processing URL (in test)Sometimes we need to send parameters to another application in addition to booting, which can also be done through custom URLs, such as:

test://
Test://com.company.test
test://config=1&abar=2

In this case, we have to do the custom processing in the launch application, and implement the message in delegate (Cocos2d added in appdelegate), for example:

-(BOOL) Application: (UIApplication *) Applicationhandleopenurl: (nsurl*) URL {//do something withthe URL here}
In general, we parse the URL from the parameter to display it in the view or store it in the userpreference. The following example stores the URL as a URL variable in user preference or prints it out:

-(BOOL) Application: (UIApplication *) application Handleopenurl: (nsurl *) URL
{
if (!url) {return NO;}
NSString *urlstring = [url absolutestring];
NSLog (@ "%@", urlstring);
[[Nsuserdefaults Standarduserdefaults] setobject:urlstring forkey:@ "url"];
[[Nsuserdefaults standarduserdefaults] synchronize];
return YES;
}

IOS---Jump and pass data between apps via OpenURL

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.