iOS 版本檢測-版本升級提示

#pragma mark 版本檢測 版本升級提示
+(void)judgeAppVersionAndShowNoticeWithView:(UIViewController*)view withAppId:(NSString*)appId
{
    NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    
    //當(dāng)前App 標(biāo)示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:@"當(dāng)前有更新版本%@可以使用, 是否前往下載。", 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];
                    }
                }
                
            }
            
        }];
    }];
    
}
//版本判斷 - 多數(shù)點(diǎn) 數(shù)字判斷
+(BOOL)justifyAppStoreVersionString:(NSString*)version withCurrentVersion:(NSString*)currentVersion
{
    int updateFlag = 0; //0:默認(rèn)相等; 1:當(dāng)前版本大于AppStore版本; 2:AppStore版本大于當(dāng)前版本-需要升級;
    //分割字符串
    NSArray *curentVersionArr = [currentVersion componentsSeparatedByString:@"."]; //當(dāng)前版本
    NSArray *appStoreVersionArr = [version componentsSeparatedByString:@"."];      //比較版本
    NSInteger count = curentVersionArr.count>appStoreVersionArr.count?appStoreVersionArr.count:curentVersionArr.count;
    
    for (int i=0; i<count; i++) {
        NSInteger curV = [[curentVersionArr objectAtIndex:i] integerValue];
        NSInteger appV = [[appStoreVersionArr objectAtIndex:i] integerValue];
        if (appV<curV) {
            updateFlag = 1;
            break;
        }else if (appV>curV){
            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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容