一.通过SKStoreProductViewController (modal 样式)实现 在当前应用内打开 App Store
1.遵守 <SKStoreProductViewControllerDelegate>
2.实现 <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];
}
3.SKStoreProductViewController只能通过 modal 方式打开,否则会报错
二.通过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];