iOS 自动提醒更新

作者: 在这蓝色天空下 | 来源:发表于2016-10-24 16:54 被阅读134次

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

//时间间隔一小时
    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、为了更好的体验效果添加一个时间间隔,不会在用户每次使用的时候都会提示有更新。

相关文章

  • iOS 自动提醒更新

    最近项目里面要添加一个提醒更新功能,在网上查了下,代码如下: 整体思路就是:1、拿当前版本的版本号跟AppStor...

  • iOS应用版本更新(自动提醒用户更新代码)

    不建议使用,使用应用版本更新功能提交App Store会被拒! 一.在#import "AppDelegate.h...

  • iOS-OC版本更新/检查更新

    自己挖的坑自己要填上...iOS是只能自动提醒更新,界面上不能有按钮提供给用户手动更新的,自己失误在设置里面的版本...

  • 关闭Firefox自动更新及更新提醒

    第一步:仅关闭自动更新,但仍存在更新提醒 第二步:彻底关闭自动更新和提醒 右击Firefox——打开Firefox...

  • 美剧更新自动提醒

    最近在追几部美剧,但是美剧网站上并没有更新提醒的功能,于是自己开发了这样的功能。大致的思路是爬取网页的特定内容,把...

  • iOS版本更新逻辑

    一、系统支持自动更新 首先iOS系统中是支持APP的自动更新的,可以开关选择 二、APP内部提示更新 若用户没有选...

  • iOS 14.4正式版发布!苹果官方提醒:必须更新!

    iOS 14.4正式版发布!苹果官方提醒:必须更新! 各位苹果用户们,iOS又双叒更新啦! 就在昨天,苹果正式向所...

  • iOS升级更新提醒功能

    苹果爸爸很早在系统设置里面提供了应用自动升级的功能,所有就明令限制了应用内弹出更新升级提醒的功能,凡是包含这个弹框...

  • 2018-08-27 itchat 新手上路

    之前客户端每次在测试阶段更新测试包之后一直在寻找一个有效的自动化提醒测试组已更新安装包方式,自动提醒邮件已经很早实...

  • iOS中如何判断版本号提示用户更新

    在进行iOS开发的时候,你时候你会提醒用户进行更新,众所周知,提醒用户进行更新时,在苹果审核的时候回被拒绝掉。所以...

网友评论

    本文标题:iOS 自动提醒更新

    本文链接:https://www.haomeiwen.com/subject/kbuluttx.html