iOS基础(八)——最新更新方法,每天只提示一次

计算,每天提示一次用户更新!
1、声明两个常量

static NSString *const UpgradeTestAlertExpireDate = @"UpgradeTestAlertExpireDate";
static NSString *const UpgradeTestAlertCount = @"UpgradeTestAlertCount";

2、更新方法

//最新更新方法
-(void)checkVersionWithAppid:(NSString *)appid completion:(void (^)(BOOL, NSString *, NSError *))completion
{
    
    NSDate *expireDate = [[NSUserDefaults standardUserDefaults] objectForKey:UpgradeTestAlertExpireDate];
    NSTimeInterval time = 60 * 60 * 24;
    //超时控制,每次启动检测版本,有新版本每天只弹出一次
    if (!expireDate) {
        expireDate = [NSDate dateWithTimeIntervalSinceNow:time];
        
        [[NSUserDefaults standardUserDefaults] setObject:expireDate forKey:UpgradeTestAlertExpireDate];
        
        [[NSUserDefaults standardUserDefaults] synchronize];
    }else {
        if ([expireDate compare:[NSDate date]] != NSOrderedDescending) {
            
            [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:UpgradeTestAlertCount];
            
            [[NSUserDefaults standardUserDefaults] setObject:[NSDate dateWithTimeIntervalSinceNow:time] forKey:UpgradeTestAlertExpireDate];
            
            [[NSUserDefaults standardUserDefaults] synchronize];
            
        }
        
    }
    __block NSInteger count = [[NSUserDefaults standardUserDefaults] integerForKey:UpgradeTestAlertCount];
    if (count >= 1) return;
    
    
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/lookup?id=%@",appid]];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    request.HTTPMethod = @"POST";
    
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        if (data == nil) {
            completion(NO,nil,error);
            return ;
        }
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        
        NSArray *array = dict[@"results"];
        
        
        __block NSString *version = nil;
        __block NSString *versionNote = nil;
        [array enumerateObjectsUsingBlock:^(NSDictionary * obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            [obj enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull dictObj, BOOL * _Nonnull stop) {
                
                if ([key isEqualToString:@"version"]) {
                    version = dictObj;
                }
                
                if ([key isEqualToString:@"releaseNotes"]) {
                    versionNote = dictObj;
                }
            }];
        }];
        
        //本地版本
        NSDictionary *infoDict = [NSBundle mainBundle].infoDictionary;
        NSString *loclaVersion = infoDict[@"CFBundleShortVersionString"];
        
        NSInteger oldVersion = [self changeNunmerWithString:loclaVersion];
        NSInteger newVersion = [self changeNunmerWithString:version];
        
        
        dispatch_async(dispatch_get_main_queue(), ^{//回到主线程
            
            if (error || array.count == 0) {
                completion(NO,nil,error);
                return ;
            }
            
            //比较版本
            if (oldVersion < newVersion) {//不是新版本
                completion(NO,versionNote,nil);
                
            }else{//当前是最新版本
                completion(YES,nil,nil);
            }
            
        });
        
        count += 1;
        [[NSUserDefaults standardUserDefaults] setInteger:count forKey:UpgradeTestAlertCount];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        
    }];
    
    //开启任务
    [task resume];
}

3、去小数点方法

/** 去掉小数 */

-(NSInteger)changeNunmerWithString:(NSString *)string
{
    NSArray *array = [string componentsSeparatedByString:@"."];
    
    NSMutableString *strM = [NSMutableString string];
    
    for (NSString *subString in array) {
        [strM appendString:subString ];
    }
    
    return [strM integerValue];
}

4、如何使用

-(void) checkNewVersion{
    //检查新版本
    WEAKSELF;
    [app checkVersionWithAppid:@"" completion:^(BOOL isNewVersion, NSString *versionInfo, NSError *error) {
        if (!isNewVersion) {
            //有新版本,弹出更新提示
            
        }
    }] ;
}

欢迎关注我的公众号

原文地址:https://www.cnblogs.com/smileK/p/9554378.html