1. 程式人生 > >搞定APP版本更新控制

搞定APP版本更新控制

做iOS開發App很久了,今天想寫寫部落格,把自己的一些想法、技術點、遇到問題寫出來,供大家參考;

app版本更新是不能自己來檢測的,否則上線的話會被拒;那如果還想自己控制版本更新的提醒怎麼辦呢,我用到了兩種方法來解決這個問題:一種是完全由前端自己控制,首先獲取到當前工程專案的版本號currentVersion,然後通過配置自己專案在商店的APPID,獲取到商店新更新的專案版本號;最後兩者做比較;一種是前端獲取使用者手機上當前版本,然後再通過調後臺介面獲取伺服器上的版本號,兩者進行比較;第二種方法的好處是後臺可以給前端傳一個欄位,然後控制強制更新還是選擇更新;廢話少說,上程式碼。

第一種:

-(

void)XFUpdateVersionApp

{

//1.先獲取當前工程專案版本號

NSDictionary *proInfoDic=[[NSBundlemainBundle]infoDictionary];

NSLog(@"%@",proInfoDic);

NSString *currentVersion=infoDic[@"BundleShortVersionString"];//BundleShortVersionString在Info.plist裡

//2.要先配置自己專案在商店的APPID

//3從網路獲取appStore版本號

NSError *error;

NSData *response = [

NSURLConnectionsendSynchronousRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:[NSStringstringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",STOREAPPID]]]returningResponse:nilerror:nil];//STOREAPPID專案的appid

if (response ==nil) {

NSLog(@"你沒有連線網路哦");

return;

    }

NSDictionary *appInfoDic = [NSJSONSerialization

JSONObjectWithData:responseoptions:NSJSONReadingMutableLeaveserror:&error];

if (error) {

NSLog(@"hsUpdateAppError:%@",error);

return;

    }

//    NSLog(@"%@",appInfoDic);

NSArray *array = appInfoDic[@"results"];

if (array.count <1) {

NSLog(@"App未提交");

return;

    }

NSDictionary *dic = array[0];

NSString *appStoreVersion = dic[@"version"];

//列印版本號

NSLog(@"當前版本號:%@\n商店版本號:%@",currentVersion,appStoreVersion);

//設定版本號

    currentVersion = [currentVersion stringByReplacingOccurrencesOfString:@"."withString:@""];

if (currentVersion.length==2) {

        currentVersion  = [currentVersion stringByAppendingString:@"0"];

    }elseif (currentVersion.length==1){

        currentVersion  = [currentVersion stringByAppendingString:@"00"];

    }

    appStoreVersion = [appStoreVersion stringByReplacingOccurrencesOfString:@"."withString:@""];

if (appStoreVersion.length==2) {

        appStoreVersion  = [appStoreVersion stringByAppendingString:@"0"];

    }elseif (appStoreVersion.length==1){

        appStoreVersion  = [appStoreVersion stringByAppendingString:@"00"];

    }

//4當前版本號小於商店版本號,就更新

if([currentVersionfloatValue] < [appStoreVersionfloatValue])

    {

UIAlertController *alercConteoller = [UIAlertControlleralertControllerWithTitle:@"版本有更新"message:[NSStringstringWithFormat:@"檢測到新版本(%@),是否更新?",dic[@"version"]]preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *actionYes = [UIAlertActionactionWithTitle:@"更新"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {

//此處加入應用在app store的地址,方便使用者去更新,一種實現方式如下

NSURL *url = [NSURLURLWithString:[NSStringstringWithFormat:@"https://itunes.apple.com/us/app/id%@?ls=1&mt=8",STOREAPPID]];

            [[UIApplicationsharedApplication]openURL:url];

        }];

UIAlertAction *actionNo = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *_Nonnull action) {

        }];

        [alercConteoller addAction:actionYes];

        [alercConteoller addAction:actionNo];

        [selfpresentViewController:alercConteolleranimated:YEScompletion:nil];

    }else{

NSLog(@"版本號好像比商店大");

    }

}

最後呼叫一下

-(void)viewDidAppear:(BOOL)animated{

    [superviewDidAppear:animated];

//一句程式碼實現檢測更新,很簡單哦 一定要在這個方法裡面呼叫

    [selfXFUpdateVersionApp];

}

這裡說明一下上面的比較版本時版本號格式是1.0.3這種單位數的格式,如果是遇到01.01.01這種格式的就可以用下面這種比較方法:

第二種:

#pragma mark - 更新版本

- (void)VersionCheck

{

//1.當前手機app版本號AppVersion本地巨集定義一個,每次發版時手動改 01.01.01格式的

//2.請求伺服器上的最新版本號 這是網路請求

    [JiangServiceApiVersionCheck:2

UsingCallback:^(DCServiceContext* context,VersionCheck* sm) {

if (context.isSucceeded && sm.result ==1) { //請求成功

NSMutableArray* versionArray = [NSMutableArrayarray];

for (VersionCheckData* objin sm.data) {

self.downStr = obj.path_add;

if (sm.data ==nil) {

                 }else {

                     [versionArrayaddObject:obj];

//當前手機自定義app版本號與伺服器上的最新版本號進行比較

if ([AppVersion2compare:obj.versionnooptions:NSNumericSearch] ==NSOrderedAscending) { //升序AppVersion<obj.versionno

//提示使用者升級

if ([obj.ppgradetypeisEqualToString:@"2"]) {//選擇更新

UIAlertView* alter = [[UIAlertViewalloc]

initWithTitle:TipTitle

message:@"您當前版本過低請及時更新"

delegate:nil

cancelButtonTitle:[selfcancelBtnTitle]

otherButtonTitles:[selfotherBtnTitle],nil];

                             alter.delegate =self;

                             [altershow];

                         }

else {// 1強制更新

UIAlertView* alter = [[UIAlertViewalloc]

initWithTitle:TipTitle

message:@"當前版本過低請更新"

delegate:nil

cancelButtonTitle:[selfcancelBtnTitle]

otherButtonTitles:nil,nil];

                             alter.delegate =self;

                             [altershow];

                         }

                     }

elseif ([AppVersion2compare:obj.versionno

options:NSNumericSearch]

                              ==NSOrderedDescending) {//降序AppVersion>obj.versionno

NSLog(@"已是高版本不需要操作");

                     }

else { // AppVersion=obj.versionno

NSLog(@"相等");不作操作

                     }

                 }

             }

         }

else {//失敗

//            [MBProgressHUD showError:@"請稍後重試"

//          toView:self.view];

         }

     }];

}