从iPhoneApplication中打开App Store的方法

通过用[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];
}
 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值