iOS 版本检测-版本升级提示

#pragma mark 版本检测 版本升级提示
+(void)judgeAppVersionAndShowNoticeWithView:(UIViewController*)view withAppId:(NSString*)appId
{
    NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    
    //当前App 标示Id
    NSString *urlStr = [NSString stringWithFormat:@"%@%@", Judge_AppVersion, appId];
    [HttpUtil sendGetRequestWithUrl:urlStr complete:^(NSError *error, NSDictionary *objectDic) {
        
        [GCDQueue executeInMainQueue:^{
            debugLog(@"获取版本信息:%@", objectDic);
            NSInteger resultCount = [[objectDic objectForKey:@"resultCount"]integerValue];
            
            if (resultCount>=1) {
                NSDictionary *results = [[objectDic objectForKey:@"results"]objectAtIndex:0];
                NSString *version = [results objectForKey:@"version"];
                
                if (version && ![version isKindOfClass:[NSNull class]] && ![version isEqualToString:@""]) {
                    
                    //版本比较
                    BOOL isNeedUpdate = [Tool justifyAppStoreVersionString:version withCurrentVersion:currentVersion];
                    if (isNeedUpdate) {
                        
                        //有新版本
                        NSString *str = [NSString stringWithFormat:@"当前有更新版本%@可以使用, 是否前往下载。", version];
                        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
                        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
                        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"升级" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                            
                            //去AppStore更新
                            NSString *appStoreLink = [NSString stringWithFormat:@"http://itunes.apple.com/cn/app/id%@",appId];
                            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:appStoreLink]];
                        }];
                        [alert addAction:cancelAction];
                        [alert addAction:okAction];
                        [view presentViewController:alert animated:YES completion:nil];
                    }
                }
                
            }
            
        }];
    }];
    
}
//版本判断 - 多数点 数字判断
+(BOOL)justifyAppStoreVersionString:(NSString*)version withCurrentVersion:(NSString*)currentVersion
{
    int updateFlag = 0; //0:默认相等; 1:当前版本大于AppStore版本; 2:AppStore版本大于当前版本-需要升级;
    //分割字符串
    NSArray *curentVersionArr = [currentVersion componentsSeparatedByString:@"."]; //当前版本
    NSArray *appStoreVersionArr = [version componentsSeparatedByString:@"."];      //比较版本
    NSInteger count = curentVersionArr.count>appStoreVersionArr.count?appStoreVersionArr.count:curentVersionArr.count;
    
    for (int i=0; icurV){
            updateFlag = 2;
            break;
        }
    }
    if (updateFlag == 0) {
        //(2.8.1 与 2.8.1.1)(2.5.3 与 2.5) 这种情况
        if (appStoreVersionArr.count>curentVersionArr.count)
        {
            updateFlag = 2;
        }
    }
    
    BOOL isNeedUpdate = updateFlag==2?YES:NO;
    return isNeedUpdate;
}

你可能感兴趣的:(iOS 版本检测-版本升级提示)