Custom URL Scheme Full guide

Source: Internet
Author: User

This article is translated by migrant from the complete Tutorial on Ios/iphone custom URL schemes, reproduced please indicate the source.

Note : Since the introduction of custom URLs, this article is always the most read article in my blog. Although most of them are the same, there are still some subtle differences in the changes. This article is an rewritten version of the original post and is updated with the latest IOS and Xcode versions.

One of the coolest features of the Iphone/ios SDK is that the app "binds" itself to a custom URL scheme that launches the app from a browser or other app.

Registering a custom URL Scheme

The first step in registering a custom URL Scheme is to create a URL scheme-find and click the project info.plist file in Xcode project Navigator. When the file is displayed in the right window, click the right mouse button on the list and select Add Row:

Scroll down the pop-up list and select URL types.

Click on the left-hand cutout to open the list and see Item 0, a dictionary entity. Expand Item 0to see the URL Identifier, a String object. The string is the name of your custom URL scheme. It is recommended to use reverse domain name method to ensure the uniqueness of the name, such as com.yourCompany.yourApp.

Click on Item 0 to add a row, select the URL schemesfrom the drop-down list and hit the keyboard enter to complete the insertion.

Note URL schemes is an array that allows the app to define multiple URL schemes.

Expand the data and click Item 0. You will define the name of the custom URL scheme here. Just need a name, do not append://-For example, if you enter Iosdevapp, your custom URL is iosdevapp://

At this point, the entire definition is as follows:

While I agree with Xcode's purpose of using descriptive names, it is also useful to see the actual key created. Here's a handy tip, right-click on plist and select Show Raw keys/valuesto see the following effects:

There is another useful output format, XML, because it is very easy to see the structure of the dictionary and the original array and the entities it includes. Click plist and select Open as–source Code:

Invoking a custom URL Scheme from Safari

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

To invoke an app using the emulator:

    • Run your app in Xcode
    • Once the application is installed, the custom URL scheme will be registered
    • Close the app by selecting Home from the emulator's Hardware menu
    • Start Safari
    • Enter the URL scheme defined earlier in the browser address bar (below)

Safari will be closed and the app will be brought back to the foreground. Congratulations, you just called an IPhone app using a custom URL scheme.

Calling custom URL Scheme from another IPhone app

Let's see how to invoke the custom URL scheme from another application. I've created a very simple IPhone app that has only one UILabel and one uibutton-that shows a piece of information telling you that the app is going to invoke another app through a custom URL scheme, and the button starts this behavior. Download source code

The code in the Buttonpressed method handles URL calls:

123456789-ten-19 
-(void)Buttonpressed:(UIButton*)button{ NSString*CustomURL=@ "iosdevtips://"; If([[UIApplicationSharedapplication] Canopenurl:[NsurlURLWithString:CustomURL]]) { [[UIApplicationSharedapplication]OpenURL:[NsurlURLWithString: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]; }}          /span>                

The 5th line of code checks to see if the custom URL is defined and, if defined, uses a shared application instance to open the URL (line 8th). OpenURL: Method launches the app and passes the URL to the app. During this process, the current app is exited.

Passing parameters to an app through custom URL Scheme

Sometimes you need to pass parameters to your app through a custom URL. Let's see how we can get this job done.

Nsurl as the basis for invoking another from one application, following the RFC 1808 (Relative Uniform Resource Locators) standard. So your familiar URL format based on Web content is also available here.

In apps that have customized URL scheme, app delegate must implement the following methods:

1234  
 - () application: (uiapplication *) application openurl:< Span class= "P" > (nsurl *) url Span class= "NF" >sourceapplication: (nsstring *) Span class= "NV" >sourceapplication annotation: () annotation           

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

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

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

In app delegate of the app that was called (set a custom URL), get the code for the parameter as follows:

123456789       
-(BOOL)Application:(UIApplication*)ApplicationOpenURL:(Nsurl*)Url Sourceapplication:(NSString*)SourceapplicationAnnotation(Id)annotation{ nslog  (@ "calling application Bundle ID:%@"  Sourceapplication);  nslog (@ "URL scheme:%@"  [url scheme]);  nslog (@ "URL query:%@" [ url query return yes;                /span>                

The output of the above code when the app is called is:

123    
 calling application Bundle id: com.3sixty. Callcustomurlurl scheme:iosdevtips url query: token= 123abct&registered= 1               

Note "Calling Application Bundle ID", which you can use to ensure that only the apps you define can interact directly with your app.

Let's change the code to verify that the Bundle ID of the app that originated the call is legitimate:

123456789    
-(BOOL)Application:(UIApplication*)ApplicationOpenURL:(Nsurl*)Url Sourceapplication:(NSString*)SourceapplicationAnnotation(Id)Annotation{ Check the calling application Bundle ID If([SourceapplicationIsequaltostring:@ "Com.3sixty.callcustomurl"]) { NSLog(@ "Calling application Bundle ID:%@"sourceapplication);  nslog (@ "URL scheme:%@" [ url scheme nslog (@ "URL query:%@"  [url query]);   return yes; } else return no;                /span>                

One thing to keep in mind is that you can't prevent other apps from calling your app through a custom URL scheme, but you can skip the next steps and return NO, just like the code above. In other words, if you want to prevent other apps from calling your app, create a different URL scheme. While this does not guarantee that your application will not be invoked, it can at least significantly reduce this possibility.

Custom URL Scheme Sample Project

I realized that it was a little more complicated to do it every step of the way. I've done two very basic iOS apps, one to customize the URL scheme, the other to call it, and pass a short list of arguments (query string). These are good entry points to experience a custom URL.

    • 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

Custom URL Scheme Full guide

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.