前言:一般來說很多時(shí)候亮蛔,我們的應(yīng)用在進(jìn)行版本更迭的時(shí)候宠互,有最新版本時(shí)畏线,都是希望能夠提醒用戶及時(shí)更新的静盅。很多時(shí)候我們在判讀是否有版本更新的時(shí)候都是利用后臺(tái)的接口進(jìn)行判斷,其實(shí)對于我們要上到appstore應(yīng)用市場的應(yīng)用來說還可以通過監(jiān)測appstore上一個(gè)已經(jīng)上線版本的版本號(hào)進(jìn)行判斷是否需要版本更新寝殴。
.h 文件
#import <Foundation/Foundation.h>
@interfaceMonitoringEditionUpdate :NSObject
+ (BOOL)isUpdateVersion;
+ (NSString*)appStoreProjectVersion;
@end
.m 文件
#import"MonitoringEditionUpdate.h"
#define storeAppID @"1234569" // 此處為自己appid?
@implementationMonitoringEditionUpdate
+ (BOOL)isUpdateVersion
{
BOOLisUpdate =NO;
NSString* currentVersion = [selfcurrentProjectVersion];
NSString* appStoreVersion = [selfappStoreProjectVersion];
currentVersion = [currentVersionstringByReplacingOccurrencesOfString:@"."withString:@""];
if (currentVersion.length==2) {
currentVersion= [currentVersionstringByAppendingString:@"0"];
}else if (currentVersion.length==1){
currentVersion= [currentVersionstringByAppendingString:@"00"];
}
appStoreVersion = [appStoreVersionstringByReplacingOccurrencesOfString:@"."withString:@""];
if (appStoreVersion.length==2) {
appStoreVersion= [appStoreVersionstringByAppendingString:@"0"];
}else if (appStoreVersion.length==1){
appStoreVersion= [appStoreVersionstringByAppendingString:@"00"];
}
if([currentVersionfloatValue] < [appStoreVersionfloatValue])
{
isUpdate =YES;
}else{
isUpdate =NO;
}
return isUpdate;
}
// 獲取當(dāng)前項(xiàng)目的版本號(hào)
+ (NSString*)currentProjectVersion
{
NSString*currentVersion =@"";
NSDictionary*infoDic=[[NSBundlemainBundle]infoDictionary];
currentVersion= infoDic[@"CFBundleShortVersionString"];//currentVersion為當(dāng)前工程項(xiàng)目版本號(hào)
return currentVersion;
}
// 獲取AppStore 版本號(hào)
+ (NSString*)appStoreProjectVersion
{
NSString* appStoreVersion =@"";
NSError*error;
NSData*response = [NSURLConnectionsendSynchronousRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:[NSStringstringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",storeAppID]]]returningResponse:nilerror:nil];
if (response == nil) {
return nil;
}
NSDictionary*appInfoDic = [NSJSONSerializationJSONObjectWithData:responseoptions:NSJSONReadingMutableLeaveserror:&error];
if (error) {
return nil;
}
NSArray*array = appInfoDic[@"results"];
if (array.count<1) {
return nil;
}
NSDictionary*dic = array[0];
//商店版本號(hào)
appStoreVersion = dic[@"version"];
return appStoreVersion;
}
@end
這樣 我們在調(diào)用的時(shí)候
- (void)compareVersion:(UIViewController*)navVC
{
if ([MonitoringEditionUpdateisUpdateVersion]) {
UIAlertController*alercConteoller = [UIAlertControlleralertControllerWithTitle:@"版本有更新"message:[NSStringstringWithFormat:@"檢測到新版本(%@),是否更新?",[MonitoringEditionUpdateappStoreProjectVersion]]preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction*actionYes = [UIAlertActionactionWithTitle:@"去更新"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {
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];
[navVCpresentViewController:alercConteolleranimated:YEScompletion:nil];
}else{
}
}