iOS app升级提醒及跳转AppStore

今天研究了一下app升级提示的流程,不难很简单。希望小白们能用得上。。。

<UIAlertViewDelegate>
/**
 *   判断app安装版本和商店版本的比较
 *
 *    PS:还是介绍一下对应信息
 *    trackCensoredName = 审查名称;
 *    trackContentRating = 评级;
 *    trackId = 应用程序 ID;
 *    trackName = 应用程序名称;
 *    trackViewUrl = 应用程序介绍网址;
 *    userRatingCount = 用户评级;
 *    userRatingCountForCurrentVersion = 1;
 *    version = 版本号;
 */
-(void)judgeAPPVersion
{
    // 快捷方式获得session对象
    NSURLSession *session = [NSURLSession sharedSession];
    NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/lookup?id=1048992038"];
    // 通过URL初始化task,在block内部可以直接对返回的数据进行处理
    NSURLSessionTask *task = [session dataTaskWithURL:url
                                    completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                        NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
                                        
                                        NSError *errorJs;
                                        id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&errorJs];
                                        NSDictionary *appInfo = (NSDictionary *)jsonObject;
                                        NSArray *infoContent = [appInfo objectForKey:@"results"];
                                        NSString *version = [[infoContent objectAtIndex:0] objectForKey:@"version"];
                                        
                                        NSLog(@"商店的版本是 %@",version);

                                        NSString *skipUrl = [[infoContent objectAtIndex:0] objectForKey:@"trackViewUrl"];
                                        
                                        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
                                        NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
                                        NSLog(@"当前的版本是 %@",currentVersion);
                                        
                                        if (![version isEqualToString:currentVersion]) {
                                            // 跳转appstore
                                            [self skipAppStore:skipUrl];
                                        }
                                        
                                    }];
    
    // 启动任务
    [task resume];
}

- (void)skipAppStore:(NSString*)strSkipUrl{
    // 初始化
    UIAlertController *alertDialog = [UIAlertController alertControllerWithTitle:@"是否更新新版本" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    // 分别创建操作
    UIAlertAction *laterAction = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        // App 跳转
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:strSkipUrl]];
    }];

    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        // 取消按键
        
    }];
    
    // 添加操作(顺序就是呈现的上下顺序)
    [alertDialog addAction:laterAction];
    [alertDialog addAction:okAction];
    
    // 呈现警告视图
    [self presentViewController:alertDialog animated:YES completion:nil];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,838评论 25 709
  • 我在与你相距遥远的南方和你聊天,忽然你说,我喜欢你,你没有发现吗?这不禁让我内心一震,想起了和你的许多事情,都变得...
    白康南阅读 767评论 0 0
  • #悦读悦美,书香校园#初读《尘埃落定》我只觉得这是本有趣的书,脑子里的印象只有傻子,聪明人。而到再读一遍,我开...
    婷婷我有少女心阅读 379评论 0 0
  • 成功不是人生的终极目标,让自己变的更好才是。 之前一直在纠结人活着到底是为什么,人生的意义到底是什么,但却终日不得...
    光年123阅读 253评论 0 1
  • 之前在后台收到一个女孩的消息问我说:最近有个还不错的男生追我,我也挺喜欢他的,但是吧,又觉得我们还没毕业,不知道能...
    茗艺堂阅读 578评论 0 1