ios端自行检查版本更新

.在这里插入图片描述
.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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值