您的位置:首页 > 移动开发 > IOS开发

iOS如何跳转到App Store下载评分页面示例代码

2017-12-06 11:22 826 查看

前言

目前很多应用是要求点击事件直接跳转到App Store,最近工作中就遇到了一个跳转 App Store 评分或者下载更新的功能,网上查到很多跳转方法,这里记录一下,下面话不多说了,来一起看看详细的介绍吧。

主要跳转方法有两种

  • 使用官方 StoreKit.framework 框架
  • 应用间跳转直接跳到 App Store 应用,并携带自己 App 的 AppID。

使用官方框架

苹果提供了StoreKit.framework框架,工程中可以导入这个框架的主头文件

#import <StoreKit/StoreKit.h>,

也可以直接导入

#import<StoreKit/SKStoreProductViewController.h >,

添加代理并实现代理方法

<SKStoreProductViewControllerDelegate>

示例代码

/**
应用内跳转到App Store页
*/
- (IBAction)jump:(id)sender {
NSString *appId = @"1066602104";
// 创建对象
SKStoreProductViewController *storeVC = [[SKStoreProductViewController alloc] init];
// 设置代理
storeVC.delegate = self;
// 初始化参数
NSDictionary *dict = [NSDictionary dictionaryWithObject:appId forKey:SKStoreProductParameterITunesItemIdentifier];
// 跳转App Store页
[storeVC loadProductWithParameters:dict completionBlock:^(BOOL result, NSError * _Nullable error) {
if (error) {
NSLog(@"错误信息:%@",error.userInfo);
}
else
{
// 弹出模态视图
[self presentViewController:storeVC animated:YES completion:nil];
}
}];
}
#pragma mark -- SKStoreProductViewControllerDelegate
/**
SKStoreProductViewControllerDelegate 方法,选择完成之后的处理
@param viewController SKStoreProductViewController
*/
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController
{
NSLog(@"将要退出 App Store 页面了");
[viewController dismissViewControllerAnimated:YES completion:^{
NSLog(@"已经退出 App Store 页面完成了");
}];
}

应用内跳转直接到 App Store 页面

此方法使用

[[UIApplication sharedApplication] openURL:url];
打开链接的方式跳转到App Store。

此方法主要是需要拿到自己要跳转 App 的 App Store 的 URL 地址

/**
直接跳转
*/
- (IBAction)justJumpToOtherPage:(id)sender {
// 应用地址
NSString *appStr = @"https://itunes.apple.com/app/apple-store/id1317248738?pt=118536605&ct=web&mt=8";
// 跳转
NSDictionary *options = @{UIApplicationOpenURLOptionUniversalLinksOnly
:@(YES)};
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:appStr] options:options completionHandler:^(BOOL success) {
NSLog(@"将要进入 App Store 页面了");
}];
}
- (IBAction)jumpToScorePage
{
NSLog(@"将要进入 App Store 评分页面了");
// 评分页面地址
NSString *scoreStr = [NSString stringWithFormat:@"https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=%@&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8",appID];
// 判断系统用对应方法
if ( @available(iOS 10 , * )) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:scoreStr] options:options completionHandler:^(BOOL success) {
NSLog(@"已经进入 App Store 页面了");
}];
} else {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:appStr]];
}
}

【注意】 跳转 App Store 需要真机运行

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息