.
.h文件
#import "AppDelegate.h"
@interface AppDelegate (VersionUpdate)
- (void)checkNewVersionUpdate;
@end
.m文件
#import "AppDelegate+VersionUpdate.h"
@implementation AppDelegate (VersionUpdate)
- (void)checkNewVersionUpdate{
WEAKSELF
[[NetWorkRequest sharedInstance] getAPPMessageWithUrl:@"http://itunes.apple.com/lookup?id=xxxxx" success:^(NSDictionary *dic) {
NSLog(@"-----------版本信息%@",dic);
NSString * version =[[[dic objectForKey:@"results"] objectAtIndex:0] valueForKey:@"version"];
if([weakSelf compareVesionWithServerVersion:version]){
NSString *alertTitle=[[@"xxxv" stringByAppendingString:[NSString stringWithFormat:@"%@",version]] stringByAppendingString:@",赶快体验最新版本吧!"];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:alertTitle preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"版本更新" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/app/idxxxxx"]];
exit(0);//退出应用程序
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"稍后更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
UIViewController *current = [weakSelf getCurrentViewControllerV];
[current.navigationController presentViewController:alert animated:YES completion:nil];
}
} failure:^(NSDictionary *error) {
NSLog(@"%@",error);
}];
}
-(BOOL)compareVesionWithServerVersion:(NSString *)version{
NSArray *versionArr = [version componentsSeparatedByString:@"."];
NSArray *currentVersionArr = [CurrentAPPVersion componentsSeparatedByString:@"."];
NSInteger a = (versionArr.count>currentVersionArr.count)?currentVersionArr.count:versionArr.count;
for (int i = 0; i < a; i++) {
NSInteger new = [versionArr[i] integerValue];
NSInteger old = [currentVersionArr[i] integerValue];
if (new>old) {
NSLog(@"有新版本");
[[NSUserDefaults standardUserDefaults] setObject:@"newVersion" forKey:@"version"];
[[NSUserDefaults standardUserDefaults] synchronize];
return YES;
}else if(new < old){
[[NSUserDefaults standardUserDefaults] setObject:@"oldVersion" forKey:@"version"];
[[NSUserDefaults standardUserDefaults] synchronize];
return NO;
}
}
[[NSUserDefaults standardUserDefaults] setObject:@"newVersion" forKey:@"version"];
[[NSUserDefaults standardUserDefaults] synchronize];
return NO;
}
//获取当前屏幕显示的viewcontroller
- (UIViewController *)getCurrentViewControllerV
{
if ([self.window.rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabBarC = (UITabBarController *)self.window.rootViewController;
UINavigationController *result = tabBarC.selectedViewController;
UIViewController *controllers = [result visibleViewController];
return controllers;
}else if ([self.window.rootViewController isKindOfClass:[UINavigationController class]]){
UINavigationController *currentNA = (UINavigationController *)self.window.rootViewController;
return currentNA.visibleViewController;
}
return nil;
}
@end