iOSApp内提示用户更新版本

2018-06-14  本文已影响12人  9bf19a4010ab

完成这个需求大体四步
1.获取App Store项目版本信息
2.获取本地版本信息
3.本地与线上版本进行对比
4.跳转App Store下载新版本

NSString *sign = [[NSUserDefaults standardUserDefaults] valueForKey:@"downLoadSign"];
NSUInteger num = [sign integerValue];
NSString *securlStr = @"https://itunes.apple.com//lookup?id=1317191959";
NSURL *url = [NSURL URLWithString:securlStr];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[NSURLConnection connectionWithRequest:req delegate:self];

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
NSError *error;
//解析
NSDictionary *appInfo = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
NSArray *infoContent = [appInfo objectForKey:@"results"];
//最新版本号
NSString *version = [[infoContent objectAtIndex:0] objectForKey:@"version"];
NSString *secVersion = [version stringByReplacingOccurrencesOfString:@"." withString:@""];
NSInteger versionNum = [secVersion integerValue];
NSLog(@"最新版本号%@",version);
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
//当前版本号
NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
NSString *secCurrentVersion = [currentVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
NSInteger secVersionNum = [secCurrentVersion integerValue];
if (secVersionNum < versionNum) {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"AppStore上有新版本可以下载" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"去下载" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSString *urlStr = @"itms-apps://itunes.apple.com/us/app/hong-xing-hui-yao/id1317191959?l=zh&ls=1&mt=8";
        NSDictionary *dic = [NSDictionary dictionary];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]  options:dic completionHandler:^(BOOL success) {
        }];
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"暂不考虑" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:sureAction];
    [alert addAction:cancel];
    UIWindow *aW = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    aW.rootViewController = [[UIViewController alloc]init];
    aW.windowLevel = UIWindowLevelAlert + 1;
    [aW makeKeyAndVisible];
    [aW.rootViewController presentViewController:alert animated:YES completion:nil];
  }
}

至于优化完善工作 就请读者根据自身项目需求来完善啦

上一篇下一篇

猜你喜欢

热点阅读