用iPhone浏覽UC浏覽器的“應用商店”時,發現可以直接在應用內打開App Store中的應用詳情和下載頁面。效果如下:
下面來看看怎麼實現這個效果吧。 蘋果官方文檔 "SKStoreProductViewController Class Reference"裡有如下介紹: 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商品詳情頁面。我按照文檔說明,寫了個例子。部分代碼如下:- (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如下代理方法: #pragma mark - SKStoreProductViewControllerDelegate - (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController { [viewController dismissViewControllerAnimated:YES completion:^{ [viewController release]; }]; } 可以這樣調用: [self openAppWithIdentifier:@"383037733"]; 這段代碼即實現了上面圖示的效果。 注:項目需要添加StoreKit框架,僅在iOS 6.0以上的設備中支持上述實現。 Framework /System/Library/Frameworks/StoreKit.framework Availability Available in iOS 6.0 and later. 如果需要兼容6.0以下的設備,可以使用下面的代碼(這種方式會跳出當前應用): - (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]; }