Launching Your Own Application via a Custom URL Scheme(在簡訊連結中開啟你的程式並且給你的程式發送訊息)

來源:互聯網
上載者:User

 

URL:http://iphonedevelopertips.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 via the info.plist file
located in your application’s project folder (NOTE: this is the same file you would 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://” would be “myapp”). It is possible for more than
one scheme to be registered by adding to this section though that would 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 various 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!

Note:

當用上述方法喚醒你的程式時,你的程式可以接收到這個URL,但如果你用URL開啟你的程式的話,這個時候就不行了。還需要如下操作一下:

-application:handleOpenURL: only gets called if your application is open and in the background. If your app's launching from a URL, -application:didFinishLaunchingWithOptions: gets called instead. You can get the URL out of that options dictionary with its
UIApplicationLaunchOptionsURLKey key, like this:

- (void)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)options
{
    NSURL *url = [options objectForKey:UIApplicationLaunchOptionsURLKey];
    if(url) // the app was launched from a URL, so we might as well hand this off
    {
        [self application:app handleOpenURL:url];
    }

    // etc.
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.