iOS 自动提醒更新

最近项目里面要添加一个提醒更新功能,在网上查了下,代码如下:

//时间间隔一小时
    NSDate *currentDate = [NSDate date];
    NSDate *userLastOpenDate =[[NSUserDefaults standardUserDefaults] objectForKey:@"AppTimeLastOpenDate"];
    NSTimeInterval timeBetween = [currentDate timeIntervalSinceDate:userLastOpenDate];
    if ((timeBetween / 60 / 60) <= 1) {
        return;
    }else{
        [[NSUserDefaults standardUserDefaults] setObject:currentDate forKey:@"AppTimeLastOpenDate"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    NSString *nowVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    
    NSString *query = [NSString stringWithFormat:@"https://itunes.apple.com/lookup?id=%@", appID];
    query = [query stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSData *jsonData = [[NSString stringWithContentsOfURL:[NSURL URLWithString:query] encoding:NSUTF8StringEncoding error:nil] dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error] : nil;
    
    NSArray *configData = [results valueForKey:@"results"];
    NSString *version = @"";
    for (id config in configData)
    {
        version = [config valueForKey:@"version"];
    }
    
    if ([version floatValue] > [nowVersion floatValue]) {
        UIAlertController *alertController=[UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"新版本%@已发布",version] message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action=[UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action){
            
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/us/app/chuang-youapp/id%@?mt=8",appID]]];
        }];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
            
        }];
        [alertController addAction:action];
        [alertController addAction:cancel];
        [self presentViewController:alertController animated:YES completion:nil];
    }

整体思路就是:
1、拿当前版本的版本号跟AppStore上的版本号去比较,如果当前版本比AppStore上的要低,说明有新版本已经更新,弹出提醒框;
2、用户点击更新,通过URL跳转到AppStore上去更新;
3、为了更好的体验效果添加一个时间间隔,不会在用户每次使用的时候都会提示有更新。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,147评论 19 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,271评论 4 61
  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 25,617评论 7 249
  • 妮子大汗淋漓的跑进候车厅,刷了车票飞奔进站,刚下完楼梯,就见动车缓缓的启动,下意识的挥手跟着跑了几步,看着动车越来...
    萍和阅读 304评论 0 0
  • 国庆和中秋其实有点不想回来,原因很简单: 不能按计划做我的事情,但前提我也要说清楚,我也是每天给家里打电话的。 不...
    丁昆朋阅读 221评论 0 1