文章来源:iOS-App版本更新提示
AppDelegate.m文件:
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
[[NSNotificationCenter defaultCenter] postNotificationName:@"notification_UpdateAPP" object:self];
}
提示更新的界面增加:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self shareAppVersionAlert];//增加版本更新功能
});
//软件更新通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shareAppVersionAlert) name:@"notification_UpdateAPP" object:nil];
#pragma mark 版本更新提示
//判断是否需要提示更新App
- (void)shareAppVersionAlert {
if(![self judgeNeedVersionUpdate]) return ;
//App内info.plist文件里面版本号
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSString *appVersion = infoDict[@"CFBundleShortVersionString"];
NSString *AppleID = @"1382531713";
// NSString *urlString = [NSString stringWithFormat:@"https://itunes.apple.com/cn/lookup?bundleId=%@", bundleId];//经过测试bundleId获取失败
NSString *urlString = [NSString stringWithFormat:@"https://itunes.apple.com/cn/lookup?id=%@", AppleID];
//两种请求appStore最新版本app信息 通过bundleId与appleId判断
//[NSString stringWithFormat:@"https://itunes.apple.com/cn/lookup?bundleid=%@", bundleId]
//[NSString stringWithFormat:@"https://itunes.apple.com/cn/lookup?id=%@", appleid]
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];
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"];
if ([self judgeNewVersion:newVersion withOldVersion:appVersion])
{
// UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示:\n您的App不是最新版本,请到App Store进行版本更新" message:@"" preferredStyle:UIAlertControllerStyleAlert];
// UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// // [alertVc dismissViewControllerAnimated:YES completion:nil];
// }];
// [alertVc addAction:action1];
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下载界面
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:sourceDict[@"trackViewUrl"]] options:@{} completionHandler:^(BOOL success) {
}];
}else{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:sourceDict[@"trackViewUrl"]]];
}
}];
[alertVc addAction:action2];
[self 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;
}
//判断当前app版本和AppStore最新app版本大小
- (BOOL)judgeNewVersion:(NSString *)newVersion withOldVersion:(NSString *)oldVersion {
NSArray *newArray = [newVersion componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"."]];
NSArray *oldArray = [oldVersion componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"."]];
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;
} else { }
}
return NO;
}
网友评论