Use a custom URL to open the app iPhone

Source: Internet
Author: User

Launching Your iPhone app via custom URL Scheme

Form http://mobiledevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html

One of the coolest features of the iPhone SDK is an application's ability to "bind" itself to a custom URL scheme and for that scheme to be used to launch itself from either a browser or from another application on the iPhone. creating this kind of binding is so simple, its almost criminal not to use it in your application!

Before you get started, you need to figure out how you want you application to respond to the URL. the simplest way to use custom schemes is to just "Wake up"; but it is also possible to pass information to the application via the URL, and in so doing, enable the application to do different things When woken up.

Registering custom URL schemes

Regardless of what you want to do once your application is started, the first step is to register a custom URL scheme with the iPhone. This is done viainfo.plistFile located in your application's project folder (note: this is the same file you wocould change to define a custom icon ).

By default, when opened, xcode will edit the file in a graphical UI. It is possible to edit the info. plist file directly in text mode which may be easier for some people.

Step 1. Right-click and "add row"

Step 2. Select "url types" as the key

Step 3. expand "Item 1" and provide a value for the URL identifier. this can be any value, but the Convention is to use a "Reverse Domain Name" (ex "com. myApp ").

Step 4. Add another row, this time to "item 1 ″.

Step 5. Select "url schemes" as the key.

Step 6. enter the characters that will become your URL scheme (e.g. "MyApp: //" wocould be "MyApp "). it is possible for more than one scheme to be registered by adding to this section though that wocould be strange thing to do.

Note: If you open the info. plist file in a text editor you will see the following has been added to the file...

CFBundleURLTypes     CFBundleURLSchemes       myapp     CFBundleURLName    com.yourcompany.myapp
Optionally handle the URL

Now that the URL has been registered. Anyone can start the application by opening a URL using your scheme.

Here are a few examples...

myapp:// myapp://some/path/here myapp://?foo=1&bar=2 myapp://some/path/here?foo=1&bar=2

The iPhone SDK, when launching the application in response to any of the URLs above, will send a message to the uiapplicationdelegate.

If you want to provide a Custom Handler, simply provide an implementation for the message in your delegate. For example:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {  // Do something with the url here}

A common technique is to parse the URL passed in and pull from it the parameters that will be used by varous views in the application and store them in the user preference. below is an example where we store the URL as a value parameter "url" in just such a manner...

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{    if (!url) {  return NO; }     NSString *URLString = [url absoluteString];    [[NSUserDefaults standardUserDefaults] setObject:URLString forKey:@"url"];    [[NSUserDefaults standardUserDefaults] synchronize];    return YES;}

Now you have everything you need to enable others to wake-up your application and even pass it information! Enjoy!

The browser on the iPhone is required.

Didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions

Lanchoptions has information when it is disabled. You can view the key in your log.

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url

Information is available at any time, including disabling and enabling, and returning home.

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.