iOS 监测版本更新利用appstore 进行判断

前言:一般来说很多时候,我们的应用在进行版本更迭的时候,有最新版本时,都是希望能够提醒用户及时更新的。很多时候我们在判读是否有版本更新的时候都是利用后台的接口进行判断,其实对于我们要上到appstore应用市场的应用来说还可以通过监测appstore上一个已经上线版本的版本号进行判断是否需要版本更新。

.h 文件

#import

@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;

}

// 获取当前项目的版本号

+ (NSString*)currentProjectVersion

{

NSString*currentVersion =@"";

NSDictionary*infoDic=[[NSBundlemainBundle]infoDictionary];

currentVersion= infoDic[@"CFBundleShortVersionString"];//currentVersion为当前工程项目版本号

return currentVersion;

}

// 获取AppStore 版本号

+ (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];

//商店版本号

appStoreVersion = dic[@"version"];

return appStoreVersion;

}

@end

这样 我们在调用的时候

- (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{

}

}

你可能感兴趣的:(iOS 监测版本更新利用appstore 进行判断)