iOS 版本更新(強制更新)檢測問題
通常iOS系統中是默認設置再wifi狀態,且網絡狀況良好下自己更新應用的.
但是如果用戶設置了不自動更新,但是我們的APP出現重要的版本,一定需要用戶更新的情況下,就會需要這個功能了.
這個版本更新一般會有兩種方式:
1.在自己的服務器上部署上一個文件,寫入版本數據,然后app去獲取版本數據,與自己的當前版本比對, 提示更新
優點:可自定義更新模式(強制更新,選擇更新)
缺點:APP審核的時間不可控
2.去AppStore獲取當前發布的版本號,與用戶當前版本比對,然后提示更新.
優點:版本更新的時間精准
缺點:自定義空間小
這兩種方法一般推薦第2種....
需要自定義更新模式的(強制更新,選擇更新)推薦使用第一種
//第二種
/*版本更新檢測
284882215 (Facebook APP ID)
//中國區,需要再.com后面加 /cn 下面已經加,測試對於外國的APP也能行
#define APP_URL @"http://itunes.apple.com/cn/lookup?id=你程序的appId"
請求網絡數據,返回的大致數據如下,其他還有好多數據,我們把關鍵的給截取出來
{
resultCount = 1;
results = (
{
artistId = 開發者 ID;
artistName = 開發者名稱;
price = 0;
isGameCenterEnabled = 0;
kind = software;
languageCodesISO2A = (
EN
);
trackCensoredName = 審查名稱;
trackContentRating = 評級;
trackId = 應用程序 ID;
trackName = 應用程序名稱";
trackViewUrl = 應用程序介紹網址(下載地址);
userRatingCount = 用戶評級;
userRatingCountForCurrentVersion = 1;
version = 版本號;
wrapperType = software;
}
);
}
取得這些數據后關鍵的信息就是“ version”最新版本號和“ trackViewUrl”程序下載地址。然后與本地程序的版本比較即可。
*/
##這個你可以參照下面第一種的過程都一樣
請求數據 -> 判斷版本 -> 提示或不提示-> 后續操作
//第一種
##你首先要在進入程序的時候 去請求這個數據
/*
responseObject的格式
{
iosDownload = "itms-apps://itunes.apple.com/cn/app/wang-yi-yun-yin-le-pao-bufm/id590338362?mt=8";
iosForce = 0; //控制強制更新
iosUpdateMsg = "更新信息,哈哈哈😄";
iosVersion = "v1.2.5";
}
*/
#pragma mark - 強制更新需要加上這個通知的監聽,就是無論怎么打開 ,都會提示更新(有點流氓,慎用)
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(forceUpVersion) name:UIApplicationDidBecomeActiveNotification object:nil];
}
#pragma mark - 判斷版本生是否更新,如有更新,提示用戶升級,的方法
-(void)versionIFUpdata
{
//是否是強制更新
BOOL force = [_responseObject[@"iosForce"] intValue];
if (force)
{
[self forceUpVersion];
}else
{
[self upVersion];
}
}
#pragma mark - 強制更新
-(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"];
if (force)
{
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:[NSString stringWithFormat:@"檢測到有新版本:%@ \n該版本是重要版本,不更新將導致應用不可用 \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 - 注意在這里跳轉到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"];
//去掉其他,只保留數字
iosVersion = [[iosVersion componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet]] componentsJoinedByString:@""];
#pragma mark - 獲取當前本地的版本號
NSDictionary *localDic =[[NSBundle mainBundle] infoDictionary];
NSString *localVersion =[localDic objectForKey:@"CFBundleShortVersionString"];
//去掉其他,只保留數字
localVersion = [[localVersion componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet]] componentsJoinedByString:@""];
int iosVersionNum = iosVersion.intValue;
int localVersionNum = localVersion.intValue;
//更新描述
NSString *detail = _responseObject[@"iosUpdateMsg"];
if (localVersionNum < iosVersionNum)//如果本地版本比較低 證明要更新版本
{
upDateflag = YES;
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"更新" message:[NSString stringWithFormat:@"檢測到有新版本: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 請求的話,需要設置一下返回格式為JSON格式
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];