做iOS開發(fā)App很久了,今天想寫寫博客,把自己的一些想法、技術(shù)點(diǎn)、遇到問題寫出來,供大家參考;
app版本更新是不能自己來檢測(cè)的,否則上線的話會(huì)被拒;那如果還想自己控制版本更新的提醒怎么辦呢,我用到了兩種方法來解決這個(gè)問題:一種是完全由前端自己控制,首先獲取到當(dāng)前工程項(xiàng)目的版本號(hào)currentVersion,然后通過配置自己項(xiàng)目在商店的APPID,獲取到商店新更新的項(xiàng)目版本號(hào);最后兩者做比較;一種是前端獲取用戶手機(jī)上當(dāng)前版本,然后再通過調(diào)后臺(tái)接口獲取服務(wù)器上的版本號(hào),兩者進(jìn)行比較;第二種方法的好處是后臺(tái)可以給前端傳一個(gè)字段,然后控制強(qiáng)制更新還是選擇更新;廢話少說,上代碼。
第一種:
-(void)XFUpdateVersionApp
{
//1.先獲取當(dāng)前工程項(xiàng)目版本號(hào)
NSDictionary*proInfoDic=[[NSBundlemainBundle]infoDictionary];
NSLog(@"%@",proInfoDic);
NSString*currentVersion=infoDic[@"BundleShortVersionString"];//BundleShortVersionString在Info.plist里
//2.要先配置自己項(xiàng)目在商店的APPID
//3從網(wǎng)絡(luò)獲取appStore版本號(hào)
NSError*error;
NSData*response = [NSURLConnectionsendSynchronousRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:[NSStringstringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",STOREAPPID]]]returningResponse:nilerror:nil];//STOREAPPID項(xiàng)目的appid
if(response ==nil) {
NSLog(@"你沒有連接網(wǎng)絡(luò)哦");
return;
}
NSDictionary*appInfoDic = [NSJSONSerializationJSONObjectWithData: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"];
//打印版本號(hào)
NSLog(@"當(dāng)前版本號(hào):%@\n商店版本號(hào):%@",currentVersion,appStoreVersion);
//設(shè)置版本號(hào)
currentVersion = [currentVersionstringByReplacingOccurrencesOfString:@"."withString:@""];
if(currentVersion.length==2) {
currentVersion? = [currentVersionstringByAppendingString:@"0"];
}elseif(currentVersion.length==1){
currentVersion? = [currentVersionstringByAppendingString:@"00"];
}
appStoreVersion = [appStoreVersionstringByReplacingOccurrencesOfString:@"."withString:@""];
if(appStoreVersion.length==2) {
appStoreVersion? = [appStoreVersionstringByAppendingString:@"0"];
}elseif(appStoreVersion.length==1){
appStoreVersion? = [appStoreVersionstringByAppendingString:@"00"];
}
//4當(dāng)前版本號(hào)小于商店版本號(hào),就更新
if([currentVersionfloatValue] < [appStoreVersionfloatValue])
{
UIAlertController*alercConteoller = [UIAlertControlleralertControllerWithTitle:@"版本有更新"message:[NSStringstringWithFormat:@"檢測(cè)到新版本(%@),是否更新?",dic[@"version"]]preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction*actionYes = [UIAlertActionactionWithTitle:@"更新"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {
//此處加入應(yīng)用在app store的地址,方便用戶去更新,一種實(shí)現(xiàn)方式如下
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*_Nonnullaction) {
}];
[alercConteolleraddAction:actionYes];
[alercConteolleraddAction:actionNo];
[selfpresentViewController:alercConteolleranimated:YEScompletion:nil];
}else{
NSLog(@"版本號(hào)好像比商店大");
}
}
最后調(diào)用一下
-(void)viewDidAppear:(BOOL)animated{
[superviewDidAppear:animated];
//一句代碼實(shí)現(xiàn)檢測(cè)更新,很簡(jiǎn)單哦一定要在這個(gè)方法里面調(diào)用
[selfXFUpdateVersionApp];
}
這里說明一下上面的比較版本時(shí)版本號(hào)格式是1.0.3這種單位數(shù)的格式,如果是遇到01.01.01這種格式的就可以用下面這種比較方法:
第二種:
#pragma mark -更新版本
- (void)VersionCheck
{
//1.當(dāng)前手機(jī)app版本號(hào)AppVersion本地宏定義一個(gè),每次發(fā)版時(shí)手動(dòng)改
01.01.01格式的
//2.請(qǐng)求服務(wù)器上的最新版本號(hào) 這是網(wǎng)絡(luò)請(qǐng)求
[JiangServiceApiVersionCheck:2
UsingCallback:^(DCServiceContext* context,VersionCheck* sm) {
if(context.isSucceeded&& sm.result==1) {//請(qǐng)求成功
NSMutableArray* versionArray = [NSMutableArrayarray];
for(VersionCheckData* objinsm.data) {
self.downStr= obj.path_add;
if(sm.data==nil) {
}else{
[versionArrayaddObject:obj];
//當(dāng)前手機(jī)自定義app版本號(hào)與服務(wù)器上的最新版本號(hào)進(jìn)行比較
if([AppVersion2compare:obj.versionnooptions:NSNumericSearch] ==NSOrderedAscending) {//升序AppVersion
//提示用戶升級(jí)
if([obj.ppgradetypeisEqualToString:@"2"]) {//選擇更新
UIAlertView* alter = [[UIAlertViewalloc]
initWithTitle:TipTitle
message:@"您當(dāng)前版本過低請(qǐng)及時(shí)更新"
delegate:nil
cancelButtonTitle:[selfcancelBtnTitle]
otherButtonTitles:[selfotherBtnTitle],nil];
alter.delegate=self;
[altershow];
}
else{// 1是強(qiáng)制更新
UIAlertView* alter = [[UIAlertViewalloc]
initWithTitle:TipTitle
message:@"當(dāng)前版本過低請(qǐng)更新"
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:@"請(qǐng)稍后重試"
//? ? ? ? ? toView:self.view];
}
}];
}