項目需求:
由于公司項目是做離線地圖的,老板希望有版本更新時用戶能及時更新,所以要求app第一次檢測到版本更新時記錄下來這次時間,如果30天后沒更新,app只能使用5次,每次進入app就彈框提示更新和稍后更新,彈出第5次以后,若還未更新則強制用戶更新,不然無法使用app,只有app更新版本后才能繼續(xù)使用
實現(xiàn)思路:
通過聯(lián)網(wǎng)檢測項目在AppStore上的版本號與本地info.plist中獲取的當前版本號對比,
若有新版本則跳轉到AppStore項目頁面下載
實現(xiàn):
從info.plist中獲取項目當前版本號:
NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
NSString *currentVersion=infoDic[@"CFBundleShortVersionString"];
發(fā)送網(wǎng)絡請求獲取App store中項目的最新版本號:
以下方法會發(fā)生請求從app store中檢測app最新的信息,拿到最新的版本號會與info.plist中版本號進行對比判斷,此方法中還封裝了一些業(yè)務邏輯:app在第一次檢測更新到30天以后,會在每次啟動app時提示版本更新和稍后更新,5次以后若還未更新則強制用戶更新,不然無法使用app,
具體根據(jù)公司的要求處理業(yè)務邏輯
下面方法也可以在application:didFinishLaunchingWithOptions:中調用
+ (void)checkAppVersionWithCompletion:(void (^)(BOOL isSuccess))completion {
[[NSUserDefaults standardUserDefaults] setObject:kCurrentVersion forKey:AppUpdateCheckCurrentVersionKey];
// 獲取appStore版本號
[[[NSURLSession sharedSession] dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://itunes.apple.com/cn/lookup?id=953032158"]] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(data && !error);
});
}
if (data == nil || error) {
return;
}
NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
NSArray *array = [appInfoDic objectForKey:@"results"];
if (array.count < 1) {
return;
}
NSDictionary *dic = [array objectAtIndex:0];
NSString *appStoreVersion = [dic objectForKey:@"version"];
#if DEBUG
appStoreVersion = @"1000000";
#endif
BOOL isNewVersion = [kCurrentVersion compare:appStoreVersion options:NSNumericSearch] == NSOrderedAscending;
if (isNewVersion) {
NSTimeInterval fristCheckAppUpdateTime = [[NSUserDefaults standardUserDefaults] doubleForKey:FristCheckAppUpdateKey];
NSTimeInterval currentTimeInterval = [[NSDate date] timeIntervalSince1970];
if (fristCheckAppUpdateTime <= 0.0) {
// 記錄第一次版本檢測到版本更新的時間
fristCheckAppUpdateTime = currentTimeInterval;
[[NSUserDefaults standardUserDefaults] setDouble:fristCheckAppUpdateTime forKey:FristCheckAppUpdateKey];
}
BOOL isTipUpdate = NO;
if (!AppUpdateCheckDayCount) {
isTipUpdate = YES;
} else {
NSTimeInterval day30LaterInterval = [NSDate getTimeIntervalWithDayCount:AppUpdateCheckDayCount fromTimeInterval:fristCheckAppUpdateTime];
// 當?shù)谝淮翁崾靖碌浆F(xiàn)在有30天了,就在每次啟動app時提示用戶更新app
isTipUpdate = currentTimeInterval >= day30LaterInterval;
}
if (isTipUpdate) {
__block NSInteger currentTipCount = [[NSUserDefaults standardUserDefaults] integerForKey:AppUpdateCheckUpdateApkWarnTipCountKey];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSString *message = nil;
NSArray *buttonTitles = @[];
if (currentTipCount >= AppUpdateCheckMaxTipCount) {
message = @"程序有最新版本,請更新并運行新版";
buttonTitles = @[@"立即更新"];
#if DEBUG
buttonTitles = @[@"立即更新", @"Reset"];
#endif
} else {
message = [NSString stringWithFormat:@"程序有最新版本,是否更新?請注意:您當前的版本還有%ld次使用機會", AppUpdateCheckMaxTipCount-currentTipCount];
buttonTitles = @[@"立即更新", @"稍后更新"];
}
currentTipCount++;
[[NSUserDefaults standardUserDefaults] setInteger:currentTipCount forKey:AppUpdateCheckUpdateApkWarnTipCountKey];
[[NSUserDefaults standardUserDefaults] synchronize];
[UIAlertView showWithTitle:@"有可用版本更新" message:message cancelButtonTitle:nil otherButtonTitles:buttonTitles tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
if (buttonIndex == 0) {
NSURL *url = [NSURL URLWithString:[@"https://itunes.apple.com/us/app/id953032158?ls=1&mt=8" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0f) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
} else {
[[UIApplication sharedApplication] openURL:url];
}
}
}
#if DEBUG
if (buttonIndex == 1 && [buttonTitles containsObject:@"Reset"]) {
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:AppUpdateCheckUpdateApkWarnTipCountKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
#endif
}];
});
} else {
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:AppUpdateCheckUpdateApkWarnTipCountKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
} else {
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:AppUpdateCheckUpdateApkWarnTipCountKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}] resume];
}
當app未更新且提示次數(shù)超過5次時就強制用戶更新,可以在applicationDidBecomeActive中調用下面的方法提示更新,攔截用戶進入app
// 強制更新
+ (void)showUpdateApp {
// 為了防止app更新完成后 啟動時獲取的currentTipCount值沒有改變,會導致再次提示一次強制更新,這里在checkAppVersion時將版本存起來,然后更新完成后再對比就不會出現(xiàn)問題
NSString *currentVersion = [[NSUserDefaults standardUserDefaults] stringForKey:AppUpdateCheckCurrentVersionKey];
BOOL isNewVersion = [currentVersion compare:kCurrentVersion options:NSNumericSearch] == NSOrderedAscending;
// 如果kCurrentVersion大于currentVersion,說明當前版本已經(jīng)更新了
if (!isNewVersion) {
// 當app未更新且提示次數(shù)超過5次時就強制用戶更新
NSInteger currentTipCount = [[NSUserDefaults standardUserDefaults] integerForKey:AppUpdateCheckUpdateApkWarnTipCountKey];
if (currentTipCount >= AppUpdateCheckMaxTipCount) {
NSArray *buttonTitles = @[@"立即更新"];
#if DEBUG
buttonTitles = @[@"立即更新", @"Reset"];
#endif
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIAlertView showWithTitle:@"有可用版本更新" message:@"程序有最新版本,請更新并運行新版" cancelButtonTitle:nil otherButtonTitles:buttonTitles tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
if (buttonIndex == 0) {
NSURL *url = [NSURL URLWithString:[@"https://itunes.apple.com/us/app/id953032158?ls=1&mt=8" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0f) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
} else {
[[UIApplication sharedApplication] openURL:url];
}
}
}
#if DEBUG
if (buttonIndex == 1 && [buttonTitles containsObject:@"Reset"]) {
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:AppUpdateCheckUpdateApkWarnTipCountKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
#endif
}];
});
}
} else {
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:AppUpdateCheckUpdateApkWarnTipCountKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}