Launching the App Store from an iPhone application

Q:  How do I launch the App Store from my iPhone application? Also, how do I link to my application on the store?

A: The -[UIApplication openURL:] method handles links to applications and media by launching the appropriate store application for the passed NSURL object. Follow the steps below to obtain a link to an application, song, or album sold on iTunes, and link to it from your iPhone application:

Launch iTunes on your computer.
Search for the item you want to link to.
Right-click or control-click on the item's name in iTunes, then choose "Copy iTunes Store URL" from the pop-up menu.
Open the modified URL using an NSURL object and the -[UIApplication openURL:] method.
Note: You can also use the iTunes Link Maker tool to get a link to an application, song, or album sold on iTunes. See iTunes Link Maker FAQ to learn more about that tool.
See Listing 1 for an example that launches the App Store from a native application.

Listing 1  Launching the App Store from an iPhone application.
NSString *iTunesLink = @"http://itunes.apple.com/us/app/id284417350?mt=8";

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesLink]];
Some iTunes links, including iTunes Affiliate links, result in multiple redirections before reaching the appropriate store application. You can process these redirects silently using NSURLConnection, and open the final URL once the redirects are complete. This allows your application to transition right to the store without launching Safari. Listing 2 demonstrates how to accomplish this.

Note: If you have iTunes links inside a UIWebView, you can use this technique after intercepting the links with the -[UIWebViewDelegate webView:shouldStartLoadWithRequest:navigationType:] delegate method.
Listing 2  Processing iTunes Affiliate links in an iPhone application.
// Process a LinkShare/TradeDoubler/DGM URL to something iPhone can handle
- (void)openReferralURL:(NSURL *)referralURL
{
    NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:referralURL] delegate:self startImmediately:YES];
    [con release];
}

// Save the most recent URL in case multiple redirects occur
// "iTunesURL" is an NSURL property in your class declaration
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response
{
    self.iTunesURL = [response URL];
    if( [self.iTunesURL.host hasSuffix:@"itunes.apple.com"])
    {
        [connection cancel];
        [self connectionDidFinishLoading:connection];
        return nil;
     }
     else
     {
       return request;
     }
}

// No more redirects; use the last URL saved
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [[UIApplication sharedApplication] openURL:self.iTunesURL];
}

你可能感兴趣的:(application)