从iPhoneApplication中打开App Store的方法

来源:互联网 发布:linux shell 等待输入 编辑:程序博客网 时间:2024/05/22 01:54

通过用[UIApplicaiont openURL:]方法打开在itunes中的URL来指向你感兴趣的应用,歌曲或者专辑。

先通过以下步骤来获取URL:

1. 在你的MAC上打开itunes。


2. 搜索你想Link到的Item。

3.右键点击或者Ctr-Click iTunes中的你感兴趣的Item,然后再添出的Menu中选择Copy iTunes Store URL,

需要注意的是你现在得到的URL是一个itunes.app.com的Link,iPhone需要的是一个可以直接连接的phoboURLs,

所以你必须进行一些转换。

4.调用[UIApplication openURL]来打开拟修改后的URL。

 

下面就是你怎么在你的Apllication中打开App Store了。


Listing 1: 直接打开URL

 NSString *iTunesLink = @"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=284417350&mt=8"[[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesLink]];


Listing 2: 动态修改URL后打开

 NSString *iTunesLink = @"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=284417350&mt=8";NSURL *iTunesURL = [NSURL URLWithString:iTunesLink]// Produce a phobos.apple.com URL that will open the iTunes or App Store application directly[NSURL URLWithString:[NSString stringWithFormat:@"http://phobos.apple.com%@?%@", iTunesURL.path, iTunesURL.query]]



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 3 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 3: 有时候一个iTunes Link有很多个指向,你可以用NSURLConnection, 打开最终的URL。

 

                 如果你有一个在UIWebView中的Link,你可以在[UIWebViewDelegate webView:shouldStartLoadWithRequest:navigationType:]这个委托中来完成这件事情

 // Process a LinkShare/TradeDoubler/DGM URL to something iPhone can handle- (void)openReferralURL:(NSURL *)referralURL {    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:referralURL] delegate:self startImmediately:YES];    [conn 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];    return request;} // No more redirects; use the last URL saved- (void)connectionDidFinishLoading:(NSURLConnection *)connection {    [[UIApplication sharedApplication] openURL:self.iTunesLink];} 

 

原创粉丝点击