iOS 判断版本是否需要更新

//判断是否需要提示更新App

- (void)shareAppVersionAlert {

   

     if(![self judgeNeedVersionUpdate])  return ;

    //App内info.plist文件里面版本号

    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];

    NSString *appVersion = infoDict[@"CFBundleShortVersionString"];

    NSString *bundleId   = infoDict[@"CFBundleIdentifier"];

    NSString *urlString =  [NSString stringWithFormat:@"https://itunes.apple.com/cn/lookup?id=1381017103"];

    NSURL *urlStr = [NSURL URLWithString:urlString];

    //创建请求体

    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:urlStr];

    [NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

        if (connectionError) {

            //            NSLog(@"connectionError->%@", connectionError.localizedDescription);

            return ;

        }

        NSError *error;

        NSDictionary *resultsDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

        NSLog(@"resultsDict +++:%@", resultsDict);

        if (error) {

            //            NSLog(@"error->%@", error.localizedDescription);

            return;

        }

        NSArray *sourceArray = resultsDict[@"results"];

        if (sourceArray.count >= 1) {

            //AppStore内最新App的版本号

            NSDictionary *sourceDict = sourceArray[0];

            NSString *newVersion = sourceDict[@"version"];

            NSLog(@"newVersion:%@",newVersion);

           

            if ([newVersion floatValue]*1000>[appVersion floatValue]*1000)

            {

                UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示:\n您的App不是最新版本,请问是否更新" message:@"" preferredStyle:UIAlertControllerStyleAlert];

                UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"暂不更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

                    //                    [alertVc dismissViewControllerAnimated:YES completion:nil];

                }];

                [alertVc addAction:action1];

                UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"去更新" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

                    //跳转到AppStore,该App下载界面

                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:sourceDict[@"trackViewUrl"]]];

                }];

                [alertVc addAction:action2];

                [[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:alertVc animated:YES completion:nil];

            }

        }

    }];

}


//每天进行一次版本判断

- (BOOL)judgeNeedVersionUpdate {

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    [formatter setDateFormat:@"yyyy-MM-dd"]

    //获取年-月-日

    NSString *dateString = [formatter stringFromDate:[NSDate date]];

    NSString *currentDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"currentDate"];

    if ([currentDate isEqualToString:dateString]) {

        return NO;

    }

    [[NSUserDefaults standardUserDefaults] setObject:dateString forKey:@"currentDate"];

    return YES;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值