IOS Custom URL Scheme full guide

Source: Internet
Author: User

Http://www.cocoachina.com/industry/20140522/8514.html

"" Reading device

Custom URL Scheme

This article was transferred from migrant's blog, the original: "The complete Tutorial on Ios/iphone custom URL schemes"

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 SchemeThe 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, right-click on the list, select Add Row: Scroll down the pop-up list and select URL types. IOS Custom URL Scheme Click on the left-hand snip to open the list and you can see Item 0, a dictionary entity. Expand Item 0 To 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. URLSCHEME2A Click on Item 0 to add a row, select the URL schemes from the drop-down list, tap the keyboard enter to complete the insertion. IOS Custom URL Scheme Note: URL schemes is an array that allows the app to define multiple URL schemes. IOS Custom URL Scheme expands the data and clicks on 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://iOS custom URL scheme at this time, the entire definition such as: iOS custom URL scheme although I I agree with Xcode's purpose of using descriptive names, but it is also useful to see the actual key created. Here's a handy tip, right-click on plist and choose Show Raw keys/values to see the following: IOS Custom URL Scheme has 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:iphone Custom URL Scheme invoking a custom URL Scheme from SafariWith 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). IOS app with Custom URL steps to invoke the app using the simulator: 1. Run App 2 in Xcode. Once the app is installed, the custom URL scheme will be registered with 3. Close the app 4 by selecting Home from the emulator's Hardware menu. Start Safari5. The URL scheme defined before the browser address bar is entered (below) Call custom URL scheme from Safari this time 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 appLet'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 IPhone app that calls the Custom URL Scheme buttonpressed method in the code handling URL call:
  1. -(void) buttonpressed: (UIButton *) button
  2. {
  3. NSString *customurl = @"iosdevtips://";
  4. if ([[UIApplication sharedapplication]
  5. Canopenurl:[nsurl Urlwithstring:customurl]])
  6. {
  7. [[UIApplication sharedapplication] Openurl:[nsurl Urlwithstring:customurl]];
  8. }
  9. Else
  10. {
  11. Uialertview *alert = [[Uialertview alloc] initwithtitle:@"URL error"
  12. Message:[nsstring stringWithFormat:
  13. @"No custom URL defined for%@", CustomURL]
  14. Delegate:self cancelbuttontitle:@"OK"
  15. Otherbuttontitles:nil];
  16. [Alert show];
  17. }
  18. }
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 SchemeSometimes 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:
    1. -(BOOL) Application: (UIApplication *) application
    2. OpenURL: (nsurl *) URL
    3. Sourceapplication: (NSString *) sourceapplication
    4. Annotation: (ID) 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:
    1. -(BOOL) Application: (UIApplication *) application OpenURL: (nsurl *) URL
    2. Sourceapplication: (NSString *) sourceapplication annotation: (id) annotation
    3. {
    4. NSLog (@"Calling application Bundle ID:%@", sourceapplication);
    5. NSLog (@"url scheme:%@", [url scheme]);
    6. NSLog (@"URL query:%@", [url query]);
    7. return YES;
    8. }
The output of the above code when the application is called is:
    1. Calling application Bundle Id:com.3sixty.callcustomurl
    2. URL scheme:iosdevtips
    3. 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:
  1. -(BOOL) Application: (UIApplication *) application OpenURL: (nsurl *) URL
  2. Sourceapplication: (NSString *) sourceapplication annotation: (id) annotation
  3. {
  4. //Check The calling application Bundle ID
  5. if ([Sourceapplication isequaltostring:@"Com.3sixty.callcustomurl"])
  6. {
  7. NSLog (@"Calling application Bundle ID:%@", sourceapplication);
  8. NSLog (@"url scheme:%@", [url scheme]);
  9. NSLog (@"URL query:%@", [url query]);
  10. return YES;
  11. }
  12. Else
  13. return NO;
  14. }
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 ProjectI 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 ResourcesHow to properly Validate URL Parameters url Scheme Reference Docs

IOS 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.