Tutorial on app jump using URL scheme in IOS _ios

Source: Internet
Author: User

The role of URL scheme

We all know that the iphone app has a sandbox, and app is an island of information that can't be communicated to each other. But iOS apps can register their URLs Scheme,url scheme is designed to make it easy for apps to call each other. We can open the app through the OpenURL of the system and pass some parameters.

For example: If you enter www.alipay.com in Safari, you can open your Alipay app directly, provided your phone is loaded with Alipay. If you have not installed Alipay, should show the Alipay download interface, click will jump to the App store's Alipay download interface.

URL scheme must uniquely identify an app, and if you set a URL scheme that conflicts with another app's URL scheme, your app will not necessarily be activated. Because when your app is installed, your URL Scheme is already registered in the system.

In general, it will invoke the first installed app. But the URL scheme for iOS's system app is definitely the highest. So when we define URL scheme, try to avoid the URL scheme that the System app has already defined.

Registering URL Scheme

1. Add URL in Info.plist types

Each project will have a info.plist configuration file. Locate Info.plist, right-click the Add Row, and select URL types. As shown in the figure:

2. Add URL Schemes

After adding URL types, click Expand. Right-select Add Row and add URL schemes:

3. Set URL schemes

Set URL schemes to Iosdevtip

4. Set URL Identifier

URL identifier is the name of a custom URL scheme, which generally uses a reverse domain name to guarantee uniqueness, such as Com.iOSStrongDemo.www

Add a successful Start prompt

To facilitate testing, we add a uialertview to the appdelegate that prompts you when the app is successfully opened:

Copy Code code as follows:

-(BOOL) Application: (UIApplication *) application Handleopenurl: (nsurl*) URL
{
Accept the passing parameters.
NSString *text = [[URL host] stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];
Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "OPEN"
Message:text
Delegate:nil
cancelbuttontitle:@ "OK"
Otherbuttontitles:nil];
[Alertview show];
return YES;
}

Safari Launch custom URL schemes APP

Since the URL schemes has been configured, then we can test the speed, we set the URL schemes is valid. Open Safari, enter in the Address bar: iosdevtip://

Successfully opened:

You can also enter in the Address bar: Iosdevtip://com.iosstrongdemo.www. It is also possible to open an app that has registered URL schemes.

Launch a URL schemes app with another app

Copy Code code as follows:

NSString *url = @ "iosdevtip://";
NSString *url = @ "Iosdevtip://com.iosstrongdemo.www";
if ([[UIApplication sharedapplication]
Canopenurl:[nsurl Urlwithstring:url]])
{
[[UIApplication sharedapplication] Openurl:[nsurl Urlwithstring:url]];
}
Else
{
NSLog (@ "Can not open URL scheme iosdevtip");
}

Open Register Iosdevtip app format is: URL scheme://url identifier, direct call URL Scheme can also open the program, URL identifier is optional.

Passing parameters to the target app via registered URL scheme

Starting the app via URL scheme is easy enough, but sometimes we want to pass some arguments when we start the app, and we can pass the arguments by URL scheme custom URL.

Yesterday we called the Proxy method of uiapplicationdelegate in Appdelegate:

Copy Code code as follows:

-(BOOL) Application: (UIApplication *) application Handleopenurl: (nsurl*) URL
{
Accept the passing parameters.
NSString *text = [[URL host] stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];
Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "OPEN"
Message:text
Delegate:nil
cancelbuttontitle:@ "OK"
Otherbuttontitles:nil];
[Alertview show];
return YES;
}

Let's take a look at Apple's note to this method:
Copy Code code as follows:

-(BOOL) Application: (UIApplication *) application Handleopenurl: (nsurl *) URL; 'll is deprecated at some point, please replace with application:openURL:sourceApplication:annotation:

This method will be discarded in the future, and it can be replaced with application:openURL:sourceApplication:annotation:.

URL parameter format

Yesterday we registered in the Iosstrongdemo URL scheme remember what it is? What you should still be impressed is the id:iosdevtip of iOS development.

Let's say we want to pass two parameters, named name and phone number, respectively, in the following format:

iosdevtip://?name=ligang&phone=13888888888

There is no déjà vu feeling. Is that how we request an interface with a Get method?

The parameters that were sent by the app that was started

Copy Code code as follows:

-(BOOL) Application: (UIApplication *) application
OpenURL: (nsurl *) URL
Sourceapplication: (NSString *) sourceapplication
Annotation: (ID) annotation
{
NSLog (@ "sourceapplication:%@", sourceapplication);
NSLog (@ "url scheme:%@", [url scheme]);
NSLog (@ "URL query:%@", [url query]);

Accept the passing parameters.
Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "OPEN"
Message:[url query]
Delegate:nil
cancelbuttontitle:@ "OK"
Otherbuttontitles:nil];
[Alertview show];

return YES;
}


When the app is started, it invokes the proxy method application:openURL:sourceApplication:annotation:. The parameter URL is the URL that launches the app, and the parameter sourceapplication is the bundle ID of the source app.

We still test it through safari and enter it in Safari's address bar: iosdevtip://?name=ligang&phone=13888888888

You can open the app to see if the parameters pass through:

Finally, let's look at the print:

2015-07-15 22:38:25.655 iosstrongdemo[9983:2894855] SourceApplication:com.apple.mobilesafari
2015-07-15 22:38:28.664 iosstrongdemo[9983:2894855] URL scheme:iosdevtip
2015-07-15 22:38:28.665 iOSStrongDemo[ 9983:2894855] URL query:name=ligang&phone=13888888888

Sourceapplication print Out is Com.apple.mobilesafari, from here you can see that it is from Safari to launch our app.

Although we have customized URL scheme, we cannot prevent others from opening our application through custom URL scheme. How to solve it?

We can specify the corresponding sourceapplication, that is, the corresponding bundle ID, through the bundle ID to determine whether we can open our app:

Copy Code code as follows:

-(BOOL) Application: (UIApplication *) application
OpenURL: (nsurl *) URL
Sourceapplication: (NSString *) sourceapplication
Annotation: (ID) annotation
{
NSLog (@ "sourceapplication:%@", sourceapplication);
NSLog (@ "url scheme:%@", [url scheme]);
NSLog (@ "URL query:%@", [url query]);

if ([Sourceapplication isequaltostring:@ "Com.3sixty.callcustomurl"]) {
Accept the passing parameters.
Uialertview *alertview = [[Uialertview alloc] initwithtitle:@ "OPEN"
Message:[url query]
Delegate:nil
cancelbuttontitle:@ "OK"
Otherbuttontitles:nil];
[Alertview show];

return YES;
}else{
return NO;
}

}


This allows us to use the bundle ID to determine whether we are allowed to open our app.

Related Article

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.