12月1日更新
謝謝,@搬磚技術(shù)的提醒
需要添加些注意事項(xiàng):
版本號(hào)對(duì)比是需要相同的位數(shù)的情況下的
比如:位數(shù)不同的沒(méi)法比較, 比如 2.3.10與2.4.1 和 2.3.9 與 2.3.10 , 這個(gè)時(shí)候 2.3.10 應(yīng)該是變成2.4.0
如果是后續(xù)需要 多位的那應(yīng)該也是 2.004.001 ,補(bǔ)足位數(shù),才有可比性.
5月 18日 更新
這部分是之前對(duì)比有BUG的,現(xiàn)在已經(jīng)修復(fù)了.

------------------------------------------------------------------------------------------
原文
通常iOS系統(tǒng)中是默認(rèn)設(shè)置再wifi狀態(tài),且網(wǎng)絡(luò)狀況良好下自己更新應(yīng)用的.
但是如果用戶設(shè)置了不自動(dòng)更新,但是我們的APP出現(xiàn)重要的版本,一定需要用戶更新的情況下,就會(huì)需要這個(gè)功能了.
這個(gè)版本更新一般會(huì)有兩種方式:
1.在自己的服務(wù)器上部署上一個(gè)文件,寫入版本數(shù)據(jù),然后app去獲取版本數(shù)據(jù),與自己的當(dāng)前版本比對(duì), 提示更新
優(yōu)點(diǎn):可自定義更新模式(強(qiáng)制更新,選擇更新)
缺點(diǎn):APP審核的時(shí)間不可控
2.去AppStore獲取當(dāng)前發(fā)布的版本號(hào),與用戶當(dāng)前版本比對(duì),然后提示更新.
優(yōu)點(diǎn):版本更新的時(shí)間精準(zhǔn)
缺點(diǎn):自定義空間小
這兩種方法一般推薦第2種....
需要自定義更新模式的(強(qiáng)制更新,選擇更新)推薦使用第一種
//第二種
/*版本更新檢測(cè)
284882215 (Facebook APP ID)
//中國(guó)區(qū),需要再.com后面加 /cn 下面已經(jīng)加,測(cè)試對(duì)于外國(guó)的APP也能行
#define APP_URL @"http://itunes.apple.com/cn/lookup?id=你程序的appId"
請(qǐng)求網(wǎng)絡(luò)數(shù)據(jù),返回的大致數(shù)據(jù)如下,其他還有好多數(shù)據(jù),我們把關(guān)鍵的給截取出來(lái)
{
resultCount = 1;
results = (
{
artistId = 開發(fā)者 ID;
artistName = 開發(fā)者名稱;
price = 0;
isGameCenterEnabled = 0;
kind = software;
languageCodesISO2A = (
EN
);
trackCensoredName = 審查名稱;
trackContentRating = 評(píng)級(jí);
trackId = 應(yīng)用程序 ID;
trackName = 應(yīng)用程序名稱";
trackViewUrl = 應(yīng)用程序介紹網(wǎng)址(下載地址);
userRatingCount = 用戶評(píng)級(jí);
userRatingCountForCurrentVersion = 1;
version = 版本號(hào);
wrapperType = software;
}
);
}
NSString *version = responseObject[@"results"][@"version"]; //版本號(hào)
NSString *trackViewUrl = responseObject[@"results"][@"trackViewUrl"]; //程序下載地址
取得這些數(shù)據(jù)后關(guān)鍵的信息就是“ version”最新版本號(hào)和“ trackViewUrl”程序下載地址。然后與本地程序的版本比較即可。
*/
##這個(gè)你可以參照下面第一種的過(guò)程都一樣
請(qǐng)求數(shù)據(jù) -> 判斷版本 -> 提示或不提示-> 后續(xù)操作
//第一種
你首先要去后臺(tái)配置這個(gè)版本更新文件,json文件,或者txt文件也行,可配置的,(后臺(tái)搞定)
在進(jìn)入程序的時(shí)候 去后臺(tái)接口,請(qǐng)求這個(gè)數(shù)據(jù)
/*
示例 josn格式
AFNetworking請(qǐng)求回來(lái)responseObject的格式 (請(qǐng)求我就不寫了,這種代碼爛大街咯)
{
iosDownload = "itms-apps://itunes.apple.com/cn/app/wang-yi-yun-yin-le-pao-bufm/id590338362?mt=8";
iosForce = 0; //控制強(qiáng)制更新
iosUpdateMsg = "更新信息,哈哈哈??";
iosVersion = "v1.2.5";
}
*/
#pragma mark - 強(qiáng)制更新需要加上這個(gè)通知的監(jiān)聽(tīng),就是無(wú)論怎么打開 ,都會(huì)提示更新(有點(diǎn)流氓,慎用)
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(versionIFUpdata) name:UIApplicationDidBecomeActiveNotification object:nil];
}
#pragma mark - 判斷版本生是否更新,如有更新,提示用戶升級(jí),的方法
-(void)versionIFUpdata
{
//是否是強(qiáng)制更新
BOOL force = [_responseObject[@"iosForce"] intValue];
if (force)
{
[self forceUpVersion]; //強(qiáng)制
}else
{
[self upVersion]; //選擇
}
}
#pragma mark - 強(qiáng)制更新
-(void)forceUpVersion
{
NSString *iosDownload;
NSString *iosVersion;
BOOL force = [_responseObject[@"iosForce"] intValue];
iosDownload = _responseObject[@"iosDownload"];
// iosDownload = @"itms-apps://itunes.apple.com/cn/app/wang-yi-yun-yin-le-pao-bufm/id590338362?mt=8";
iosVersion = _responseObject[@"iosVersion"];
//更新描述
NSString *detail = _responseObject[@"iosUpdateMsg"];
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:[NSString stringWithFormat:@"檢測(cè)到有新版本:%@ \n該版本是重要版本,不更新將導(dǎo)致應(yīng)用不可用 \n%@",iosVersion,detail] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// itms-apps://itunes.apple.com/cn/app/wang-yi-yun-yin-le-pao-bufm/id590338362?mt=8
NSURL *url = [NSURL URLWithString:iosDownload];
[[UIApplication sharedApplication] openURL:url];
upDateflag = NO;
}];
[alertVC addAction:action1];
[self presentViewController:alertVC animated:YES completion:nil];
}
#pragma mark - 選擇更新
-(void)upVersion
{
NSString *iosDownload;
NSString *iosVersion;
iosDownload = _responseObject[@"iosDownload"];
#pragma mark - 注意在這里跳轉(zhuǎn)到APPstore需要URL Schemes :"itms-apps://............."
// iosDownload = @"itms-apps://itunes.apple.com/cn/app/wang-yi-yun-yin-le-pao-bufm/id590338362?mt=8";
iosVersion = _responseObject[@"iosVersion"];
//去掉其他,只保留數(shù)字
iosVersion = [[iosVersion componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet]] componentsJoinedByString:@""];
#pragma mark - 獲取當(dāng)前本地的版本號(hào)
NSDictionary *localDic =[[NSBundle mainBundle] infoDictionary];
NSString *localVersion =[localDic objectForKey:@"CFBundleShortVersionString"];
//去掉其他,只保留數(shù)字
localVersion = [[localVersion componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet]] componentsJoinedByString:@""];
//這里需要補(bǔ)齊位數(shù),保證 版本號(hào)位數(shù)相同的情況下再進(jìn)行比較
if(iosVersion.length != localVersion.length){
NSInteger gap = 0;
if(iosVersion.length > localVersion.length){
gap = iosVersion.length - localVersion.length;
while (gap) {
localVersion = [localVersion stringByAppendingString:@"0"];
gap--;
}
}else{
gap = localVersion.length - iosVersion.length;
while (gap) {
iosVersion = [iosVersion stringByAppendingString:@"0"];
gap--;
}
}
}
int iosVersionNum = iosVersion.intValue;
int localVersionNum = localVersion.intValue;
//更新描述
NSString *detail = _responseObject[@"iosUpdateMsg"];
if (localVersionNum < iosVersionNum)//如果本地版本比較低 證明要更新版本
{
upDateflag = YES;
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"更新" message:[NSString stringWithFormat:@"檢測(cè)到有新版本:v%@ \n %@",iosVersion,detail] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// itms-apps://itunes.apple.com/cn/app/wang-yi-yun-yin-le-pao-bufm/id590338362?mt=8
NSURL *url = [NSURL URLWithString:iosDownload];
[[UIApplication sharedApplication] openURL:url];
upDateflag = NO;
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
upDateflag = NO;
}];
[alertVC addAction:action1];
[alertVC addAction:action2];
[self presentViewController:alertVC animated:YES completion:nil];
}
}
//如果是使用AFNetworking 請(qǐng)求的話,需要設(shè)置一下返回格式為JSON格式
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
根據(jù)@ylyer的反饋,測(cè)試了一下 , "http://itunes.apple.com/lookup?id=我的appid&country=cn "這個(gè)樣子也是可以成功的