版本更新檢查
#pragma mark - 保存應用在AppStore上的版本號到本地 + (void)saveAppStoreVersionToUserDefaults { NSString *storeVersion = [kUserDefaults stringForKey:kAppStoreVersionKey]; NSString *bundleVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]; // 應用當前的version,應該小於等於store上的version。如果不是,則說明應用升級後,UserDefault中保存的store version未更新,需重新設。 if(nil == storeVersion || [self version:bundleVersion isBiggerThan:storeVersion]) { storeVersion = [HYBNetworkEngine obtainLatestAppVersion]; // 獲取最新的版本 if (storeVersion) { [kUserDefaults setObject:storeVersion forKey:kAppStoreVersionKey]; } } return; } #pragma mark - 是否需要更新應用 + (BOOL)isAppNeedToUpdate:(BOOL)needNetwork { NSString *version = nil; if (needNetwork) { // 獲取應用在appStore上的版本 version = [HYBNetworkEngine obtainLatestAppVersion]; if (version) { // 保存到本地 [kUserDefaults setObject:version forKey:kAppStoreVersionKey]; } } else { // 直接從本地獲取 version = [kUserDefaults stringForKey:kAppStoreVersionKey]; } if (!version) { return NO; } NSString *bundleVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]; if ([self version:version isBiggerThan:bundleVersion]) { return YES; } return NO; } + (BOOL)version:(NSString *)versionA isBiggerThan:(NSString *)versionB { NSArray *a = [versionA componentsSeparatedByString:@"."]; NSArray *b = [versionB componentsSeparatedByString:@"."]; unsigned aa = [[a objectAtIndex:0] intValue]; unsigned ab = [a count] > 1 ? [[a objectAtIndex:1] intValue] : 0; unsigned cc = [a count] > 2 ? [[a objectAtIndex:2] intValue] : 0; unsigned ba = [[b objectAtIndex:0] intValue]; unsigned bb = [b count] > 1 ? [[b objectAtIndex:1] intValue] : 0; unsigned bc = [b count] > 2 ? [[b objectAtIndex:2] intValue] : 0; return ((aa > ba) || (aa == ba && ab > bb) || (aa == ba && ab == bb && cc > bc)); } #pragma mark - 進入AppStore應用 + (void)goToAppStore { #if TARGET_IPHONE_SIMULATOR NSLog(@"虛擬機不支持APP Store.打開iTunes不會有效果。"); #else NSString *iTunesLink = [NSString stringWithFormat:@"https://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=%@&mt=8", kAppIDInAppStore, nil]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesLink]]; #endif }
最後更新:2017-04-03 08:26:24