欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

iOS应用内打开App Store应用详情界面

来源: 开发者 投稿于  被查看 1635 次 评论:272

iOS应用内打开App Store应用详情界面


    用iPhone浏览UC浏览器的“应用商店”时,发现可以直接在应用内打开App Store中的应用详情和下载页面。效果如下:
 
 
        下面来看看怎么实现这个效果吧。
 
        苹果官方文档 "SKStoreProductViewController Class Reference"里有如下介绍:
 
[plain]  
A SKStoreProductViewController object presents a store that allows the user to purchase other media from the App Store. For example, your app might display the store to allow the user to purchase another app.  
  
To display a store, create a new SKStoreProductViewController object and set its delegate. Then, present the view controller modally from another view controller in your app. Your delegate dismisses the view controller when the user completes the purchase.  
  
To choose a specific product, call the loadProductWithParameters:completionBlock: method, passing the iTunes item identifier for the item you want to sell.  
 
        由上可知,通过Modal view方式弹出App Store商品详情页面。我按照文档说明,写了个例子。部分代码如下:
[java]  
- (void)openAppWithIdentifier:(NSString *)appId {  
    SKStoreProductViewController *storeProductVC = [[SKStoreProductViewController alloc] init];  
    storeProductVC.delegate = self;  
      
    NSDictionary *dict = [NSDictionary dictionaryWithObject:appId forKey:SKStoreProductParameterITunesItemIdentifier];  
    [storeProductVC loadProductWithParameters:dict completionBlock:^(BOOL result, NSError *error) {  
        if (result) {  
            [self presentViewController:storeProductVC animated:YES completion:nil];  
        }  
    }];  
}  
 
另外,需要实现SKStoreProductViewControllerDelegate如下代理方法:
[java] 
#pragma mark - SKStoreProductViewControllerDelegate  
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {  
    [viewController dismissViewControllerAnimated:YES completion:^{  
        [viewController release];  
    }];  
}  
 
 
可以这样调用:
[java]  
[self openAppWithIdentifier:@"383037733"];  
 
这段代码即实现了上面图示的效果。
注:项目需要添加StoreKit框架,仅在iOS 6.0以上的设备中支持上述实现。
 
[java]  
Framework     
/System/Library/Frameworks/StoreKit.framework  
Availability      
Available in iOS 6.0 and later.  
 
如果需要兼容6.0以下的设备,可以使用下面的代码(这种方式会跳出当前应用):
 
[java] 
- (void)outerOpenAppWithIdentifier:(NSString *)appId {  
    NSString *urlStr = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/us/app/id%@?mt=8", appId];  
    NSURL *url = [NSURL URLWithString:urlStr];  
    [[UIApplication sharedApplication] openURL:url];  
}  
 

相关文章

    暂无相关文章

用户评论