iOS 怎么在app里提示版本更新

13 篇文章 2 订阅
#pragma mark - 检查版本更新
+(void)checkVersion {
    //    if (![self judgeNeedVersionUpdate]) {
    //        return;
    //    }
    NSString *localVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    
    NSString *urlStr = @"https://itunes.apple.com/cn/lookup?id=xxx"; //xxx-你的Appid,iTunes可查看
    
    [[HDNetworking sharedHDNetworking] GET:urlStr parameters:nil success:^(id  _Nonnull responseObject) {
        int code = [responseObject[@"resultCount"] intValue];
        if (code == 1) {
            NSArray *sourceArray = responseObject[@"results"];
            if (sourceArray.count >= 1) {
                //AppStore内最新App的版本号
                NSDictionary *sourceDict = sourceArray[0];
                NSString *newVersion = sourceDict[@"version"];
                if ([self judgeNewVersion:newVersion withOldVersion:localVersion])
                {
                    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"版本更新" message:sourceDict[@"releaseNotes"] preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"暂不更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        
                    }];
                    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"去更新" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                        //跳转到AppStore下载界面
                        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:sourceDict[@"trackViewUrl"]]];
                    }];
                    [alertVc addAction:action1];
                    [alertVc addAction:action2];
                    [[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:alertVc animated:YES completion:nil];
                }
            }
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}
//每天进行一次版本判断
- (BOOL)judgeNeedVersionUpdate {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateString = [formatter stringFromDate:[NSDate date]];
    NSString *currentDate = [UserDefaultsUtils getCurrentDate];
    if ([currentDate isEqualToString:dateString]) {
        return NO;
    }
    [UserDefaultsUtils setCurrentDate:dateString];
    return YES;
}
/*!
 @brief 判断当前app版本和AppStore最新app版本大小
 @param newVersion AppStore版本号
 @param oldVersion 本地APP版本号
 @return YES为AppStore版本大于本地APP版本
 */
- (BOOL)judgeNewVersion:(NSString *)newVersion withOldVersion:(NSString *)oldVersion {
    NSArray *newArray = [newVersion componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"."]];
    NSArray *oldArray = [oldVersion componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"."]];
    if (oldArray.count == newArray.count) {
        for (NSInteger i = 0; i < newArray.count; i ++) {
            if ([newArray[i] integerValue] > [oldArray[i] integerValue]) {
                return YES;
            } else if ([newArray[i] integerValue] < [oldArray[i] integerValue]) {
                return NO;
            }
        }
    }
    return NO;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值