方式一、通过SKStoreProductViewController (modal 样式)实现 在当前应用内打开 App Store
SKStoreProductViewController.gif
- 遵守 <SKStoreProductViewControllerDelegate>
- 实现 <SKStoreProductViewControllerDelegate>
#pragma mark - Download HLM in SKStoreProductViewController
- (void)openAppFromAppStore:(NSString *)appid {
if (nil == appid || appid.length <= 0) {
return;
}
//loading
[SVProgressHUD show];
[SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
// // 改变导航栏的文字和图片颜色
// [[UINavigationBar appearance] setTintColor:[UIColor greenColor]];
// // 红色导航栏
// [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
// [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[SKStoreProductViewController class]]];
//
SKStoreProductViewController *store = [[SKStoreProductViewController alloc] init];
store.delegate = self;
NSDictionary<NSString *, id> *parameters = @{SKStoreProductParameterITunesItemIdentifier: appid};
[store loadProductWithParameters:parameters completionBlock:^(BOOL result, NSError *error) {
// finish loading
[SVProgressHUD dismiss];
if (error) {
NSLog(@"error %@ with userInfo %@", error, [error userInfo]);
// 提示用户发生了错误
// 或者通过 URL 打开 AppStore App.
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/in/app/id%@",HLMAPPID]];
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:NULL];
} else {
[self presentViewController:store animated:YES completion:NULL];
}
}];
}
/// 用户点击取消会执行该方法
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
[viewController dismissViewControllerAnimated:YES completion:NULL];
}
- SKStoreProductViewController只能通过 modal 方式打开,否则会报错
//Terminating app due to uncaught exception 'SKUnsupportedPresentationException',
//reason: 'SKStoreProductViewController must be used in a modal view controller'
方式二、通过openURL:options:completionHandler:
方式,使用 scheme 为 itms-apps://
(push 样式的) 启动App Store应用 打开下载界面
//push 样式的 App Store应用 打开下载界面 scheme > itms-apps://
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id%@",HLMAPPID]];
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:NULL];
方式三、通过openURL:options:completionHandler:
方式,使用 scheme 为 https://
(push 样式的) 启动App Store应用 打开下载界面
//push 样式的 App Store应用 打开下载界面 scheme > https://
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/in/app/id%@",HLMAPPID]];
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:NULL];
测试用Demo