iOS 开发中的点点滴滴

Xcode 版本自动更新提示 (取消按钮取消则为强制升级)

2019-01-15  本文已影响0人  路人甲_kbb
#import "AppDelegate+UpdateVersion.h"

#define APPID @""

@implementation AppDelegate (UpdateVersion)

-(void)GetAPPStroeVersion{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *getVer = [NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",APPID];
    //    NSString *getVer = [NSString stringWithFormat:@"https://itunes.apple.com/cn/app/sanweizhijia/id%@?mt=8",APPID];
    
    [manager POST:getVer parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *array = responseObject[@"results"];
        if (array.count > 0 ) {//没有获取到数据
            NSDictionary *dict = [array lastObject];
            NSLog(@"当前appstore版本为:%@", dict[@"version"]);
            NSString *newVer = dict[@"version"];
            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
            NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
            NSLog(@"当前app版本为:%@",app_Version);
            
            [self versionCompareOldVersion:app_Version andVersionNewVersion:newVer];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}

//======  版本号比较===
// 方法调用

/*
 * OldVersion  旧版本号
 * NewVersion 新版本号
 *
 */
- (void)versionCompareOldVersion:(NSString *)OldVersion andVersionNewVersion: (NSString *)NewVersion
{
    NSArray *versions1 = [OldVersion componentsSeparatedByString:@"."];
    NSArray *versions2 = [NewVersion componentsSeparatedByString:@"."];
    NSMutableArray *ver1Array = [NSMutableArray arrayWithArray:versions1];
    NSMutableArray *ver2Array = [NSMutableArray arrayWithArray:versions2];
    // 确定最大数组
    NSInteger a = (ver1Array.count> ver2Array.count)?ver1Array.count : ver2Array.count;
    // 补成相同位数数组
    if (ver1Array.count < a) {
        for(NSInteger j = ver1Array.count; j < a; j++)
        {
            [ver1Array addObject:@"0"];
        }
    }
    else
    {
        for(NSInteger j = ver2Array.count; j < a; j++)
        {
            [ver2Array addObject:@"0"];
        }
    }
    // 比较版本号
    int result = [self compareArray1:ver1Array andArray2:ver2Array];
    if(result == 1)
    {
        NSLog(@"V1 > V2");
    }
    else if (result == -1)
    {
        NSLog(@"V1 < V2");
        [self updateVersion];
    }
    else if (result ==0 )
    {
        NSLog(@"V1 = V2");
    }
}
// 比较版本号
- (int)compareArray1:(NSMutableArray *)array1 andArray2:(NSMutableArray *)array2
{
    for (int i = 0; i< array2.count; i++) {
        NSInteger a = [[array1 objectAtIndex:i] integerValue];
        NSInteger b = [[array2 objectAtIndex:i] integerValue];
        if (a > b) {
            return 1;
        }
        else if (a < b)
        {
            return -1;
        }
    }
    return 0;
}

/*
 * 版本更新提示
 */

-(void)updateVersion{
    NSString *msg = [NSString stringWithFormat:@"有新版本,请到AppStore更新最新版本!"];
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"升级提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *CancleAction = [UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:^(UIAlertAction*action) {
        
        //        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/cn/app/id%@?mt=8",APPID]];
        //        [[UIApplication sharedApplication]openURL:url];
    }];
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"现在升级"style:UIAlertActionStyleDestructive handler:^(UIAlertAction*action) {
        
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/cn/app/id%@?mt=8",APPID]];
        [[UIApplication sharedApplication]openURL:url];
    }];
    [alertController addAction:CancleAction];
    [alertController addAction:otherAction];
    [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
    
}
@end
上一篇下一篇

猜你喜欢

热点阅读