简述:
本文讲解iOS系统框架StoreKit中的SKStoreProductViewController与SKStoreReviewController这两个Controller。
SKStoreProductViewController
这个是系统自带的控制器类,用于在我们的应用内展示AppStore中App的界面,是iOS6.0之后系统对我们开放的。
内容不多,就一个代理与一个对象方法。
// Delegate for product page events
@property(nonatomic, assign, nullable) id <SKStoreProductViewControllerDelegate> delegate NS_AVAILABLE_IOS(6_0);
这个代理是必须遵守的,因为我们需要用到它的代理方法。
// Load product view for the product with the given parameters. See below for parameters (SKStoreProductParameter*).
// Block is invoked when the load finishes.
- (void)loadProductWithParameters:(NSDictionary<NSString *, id> *)parameters completionBlock:(nullable void(^)(BOOL result, NSError * __nullable error))block NS_AVAILABLE_IOS(6_0);
loadProductWithParameters:completionBlock:接收两个参数:(1)一个字典:用一个key指定我们想要显示给用的程序的标示符。(2)一个completion block。
这个是加载方法,需要执行这个方法后,控制器才会有内容,否则就是一片空白,如下图:
// Sent if the user requests that the page be dismissed
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController __TVOS_PROHIBITED NS_AVAILABLE_IOS(6_0);
这个是代理方法,控制器显示完成后,左上角的取消按钮的点击回调方法。
用法代码:
这个类,内容不多,所以用法也很简单。
SKStoreProductViewController *product = [[SKStoreProductViewController alloc] init];
product.delegate = self;
__weak typeof(self) weakSelf = self;
[product loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier : @"要显示的App的ID"} completionBlock:^(BOOL result, NSError * _Nullable error) {
if (error) {
DMLog(@"result: %d, error: %@", result, error.localizedDescription);
} else {
[weakSelf presentViewController:product animated:YES completion:nil];
}
}];
注意一点: SKStoreProductViewController以模态方式显示。当用户打算离开App Store时我们负责解除(隐藏)SKStoreProductViewController。
然后就是代理方法,当用户点击左上角的取消按钮时的回调处理。
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
[viewController dismissViewControllerAnimated:YES completion:nil];
}
注意: 这个方法必须执行,否则当用户点击取消按钮时将毫无反应。
要显示的App的id的获取方法
1. 如果要显示的是你或你们自己开发的App,可以在iTunes Connect中查到。
步骤:登录iTunes Connect -> 点击我的App -> 点击要显示的App ->
2. 如果是要展示别人的App,可以在iTunes中取到App的链接,链接中有该App的ID。
这里以QQ为例:
在搜索框中输入要显示的App的名称,选择在iTunes Store搜索:
拷贝出来的链接大致为:https://itunes.apple.com/cn/app/qq/id44****666?mt=8
id后面的44****666就是该App的AppID。
SKStoreReviewController
这个是近期才出来的,iOS10.3开始才有的,用来在App应用内部为App评星级的控制器。
注意:这个Controller类是Xcode8.3的StoreKit中才有的,在Xcode8.3之下会报错;并且只对iOS10.3系统才生效,10.3之下没有任何反应。
/** Request StoreKit to ask the user for an app review. This may or may not show any UI.
*
* Given this may not succussfully present an alert to the user, it is not appropriate for use
* from a button or any other user action. For presenting a write review form, a deep link is
* available to the App Store by appending the query params "action=write-review" to a product URL.
*/
+ (void)requestReview;
可以看见它只有这么一个类方法。
用法代码:
[SKStoreReviewController requestReview];
在要显示的地方加入这句代码即可。