Full guide to custom URL Scheme and full guide to scheme

Source: Internet
Author: User

Complete Guide for custom URL Scheme (reprinted) and complete guide for scheme

One of the coolest features of the iPhone/iOS SDK is that the app "binds" itself to a custom URL scheme, which is used to start the app from a browser or other app.

Register a custom URL Scheme

The first step to register a custom URL Scheme is to create a URL Scheme. In Xcode Project Navigator, locate and click the Project info. plist file. When the file is displayed in the right window, right-click the list and selectAdd Row:

Scroll down the pop-up list and selectURL types.

Click the cut header on the left to open the list.Item 0, A dictionary entity. ExpandItem 0, You can seeURL Identifier, A String object. This string is the name of your custom URL scheme. We recommend that you use the Reverse Domain Name method to ensure the uniqueness of the name, for exampleCom. yourCompany. yourApp.

ClickItem 0Add a row and select from the drop-down listURL SchemesAnd press the Enter key to complete the insertion.

Note that URL Schemes is an array that allows the application to define multiple URLs.

Expand the data and clickItem 0. Here you will define the name of the custom URL scheme. You only need a name and do not append the following: //-for example, if you enter iOSDevApp, your custom url is iOSDevApp ://

The entire definition is as follows:

Although I agree with Xcode's purpose of using descriptive names, it is very useful to see the actually created key. Here is a convenient technique. Right-click plist and selectShow Raw Keys/ValuesTo see the following results:

There is also another useful output format, XML, because it is very easy to see the dictionary and the original array and Its Structure of the entity. Click plist and selectOpen As-Source Code:

Call the custom URL Scheme from Safari

With the URL scheme defined, we can run a quick test to verify whether the application is called as expected. Before that, I created a quasi-UI to identify an application with a custom URL. The App has only one UILabel With the text "App With m url ". Download source code

To call an application using a simulator, follow these steps:

  • Run the application in Xcode
  • Once the application is installed, the custom URL scheme will be registered.
  • Use Home in the hardware menu of the simulator to disable the application.
  • Start Safari
  • Enter the previously defined URL scheme (as follows) in the address bar of the browser)

At this time, Safari will be disabled and the application will be taken back to the front-end. Congratulations! You just called an iPhone app using a custom URL scheme.

Call the custom URL Scheme from another iPhone app

Let's see how to call the custom URL scheme from another application. I created another very simple iPhone app, which only has one UILabel and one UIButton-the former displays a piece of information, telling you that this app will call another app through a custom URL scheme, button to start this action. Download source code

The code in the buttonPressed method processes URL calls:

12345678910111213141516171819
- (void)buttonPressed:(UIButton *)button{  NSString *customURL = @"iOSDevTips://";  if ([[UIApplication sharedApplication]    canOpenURL:[NSURL URLWithString:customURL]])  {    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:customURL]];  }  else  {    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"URL error"                          message:[NSString stringWithFormat:                            @"No custom URL defined for %@", customURL]                          delegate:self cancelButtonTitle:@"Ok"                          otherButtonTitles:nil];    [alert show];  }}

The 5th line code checks whether the custom URL is defined. If yes, use the shared application instance to open the URL (row 8th ). OpenURL: Method to start the application and pass the URL to the application. In this process, the current application is exited.

Pass parameters to the application through the custom URL Scheme

Sometimes you need to pass parameters to the application through a custom URL. Let's see how to complete this job.

NSURL, as the basis for calling another application, complies with RFC 1808 (Relative Uniform Resource Locators) standards. Therefore, the URL format you are familiar with is also applicable here.

In a custom URL scheme Application, app delegate must implement the following methods:

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

The trick to passing parameters from one application to another is through URL. For example, if we use the following URL scheme and want to pass a parameter named "token" and a sign that identifies the registration status, we can create a URL like this:

1
NSString *customURL = @"iOSDevTips://?token=123abct&registered=1";

In web development, the string? Token= 123 abct & registered = 1It is called a query string ).

In the app delegate of an application called (with a custom URL set), the Code for obtaining parameters is as follows:

123456789
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url        sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{  NSLog(@"Calling Application Bundle ID: %@", sourceApplication);  NSLog(@"URL scheme:%@", [url scheme]);  NSLog(@"URL query: %@", [url query]);  return YES;}

The output of the above Code when the application is called is:

123
Calling Application Bundle ID: com.3Sixty.CallCustomURLURL scheme:iOSDevTipsURL query: token=123abct&registered=1

Note "Calling Application Bundle ID". You can use this to ensure that only the defined Application can directly interact with your Application.

Let's change the code to verify that the Bundle ID of the application that initiates the call is legal:

123456789101112131415
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url        sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{  // Check the calling application Bundle ID  if ([sourceApplication isEqualToString:@"com.3Sixty.CallCustomURL"])  {    NSLog(@"Calling Application Bundle ID: %@", sourceApplication);    NSLog(@"URL scheme:%@", [url scheme]);    NSLog(@"URL query: %@", [url query]);    return YES;  }  else    return NO;}

Note that you cannot prevent other applications from calling your application through the custom URL scheme. However, you can skip subsequent operations and return NO, just like the code above. That is to say, if you want to prevent other applications from calling your application, create a distinctive URL scheme. Although this does not guarantee that your application will not be called, it at least greatly reduces this possibility.

Custom URL Scheme example project

I realized that every step of this article is still a little complicated. I have completed two very basic iOS applications, one customized URL scheme, the other called it, and passed a relatively short list of parameters (query string ). These are good entry points for user-defined URLs.

  • Download Xcode project for app with Custom URL scheme
  • Download Xcode project for app to call custom URL scheme
Other resources

How to Properly Validate URL Parameters URL Scheme Reference Docs

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.