获取App Store中版本号,有更新则提示弹窗

如题, 今天我来给大家介绍:

一, 如何从App Store获取自己APP的版本号;

二, 如何判断本地版本与App Store上的版本大小;

三, 如何在项目中进行弹窗显示;

现在开始讲解:

一, 如何从App Store获取自己APP的版本号:

1. 获取本地版本号:

NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];

NSString *version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];

2, 获取App Store上版本号:

/* -----------  id为App Store内app的id  -----------*/

2.1, 如何获取自己app的id:

去App Store搜索自己的app --> 复制链接 -->随便找块地粘贴链接, 上面有id值 -->copy出来即可

2.2, App Store上的链接地址都是 http://itunes.apple.com/cn/lookup?id=%@

#pragma mark - 获取是否是最新版本

+(void)getVersonForAppCompate:(void (^)(BOOL isSuccess,NSString *version,BOOL isNew))compate{

NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];

NSString *version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];

/* -----------  id为App Store内app的id  -----------*/

[YLDAFNManager POST:@"http://itunes.apple.com/cn/lookup?id=1125234448" withToken:nil parameters:nil success:^(id  _Nullable responseObject) {

NSData *data = (NSData *)responseObject;

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers  error:nil];

NSArray *array = dic[@"results"];

if (array.count > 0) {

NSDictionary *dict = [array lastObject];

NSString *ver = dict[@"version"];

/* ----------- 下面要介绍的对比方法  -----------*/

BOOL isLarge = [NSString checkVersionForAppstoreVer:ver andInfoVersion:version];

compate(YES,ver,isLarge);

NSLog(@"当前本地版本为:%@, App Store版本:%@,是否有更新:%zd", version,ver,isLarge);

}else{

compate(NO,version,NO);

}

} failure:^(NSError * _Nonnull error) {

compate(NO,version,NO);

NSLog(@"error == %@",error);

}];

}

二, 如何判断本地版本与App Store上的版本大小;

如下: 我只复制提供有两个小数点的版本判断, 取出了第一段,第二段,第三段数字进行判断大小; 每一段不限制长度;

如果,你的版本有三个及以上的小数点: 则你需要NSRange截取的时候options:NSBackwardsSearch 这里可以选择查找的起始地点: 这里我不做详细解说了, 自己去查找NSRange的截取方法, 截出你想要的结果

#pragma mark - 判断版本是否最新 前者Appstore版本  后者本地版本 注:仅支持(两个点号的版本字符串) xxxx.xxxx.xxx

+(BOOL)checkVersionForAppstoreVer:(NSString *)appstoreVer andInfoVersion:(NSString *)infoVersion{

NSRange range = [appstoreVer rangeOfString:@"."]; //第一个点的位置

NSRange lastRange = [appstoreVer rangeOfString:@"." options:NSBackwardsSearch]; //第二个点的位置

NSInteger firstAppNum = [[appstoreVer substringWithRange:NSMakeRange(0, range.location)] integerValue]; //第一段数字

NSInteger secAppNum = [[appstoreVer substringWithRange:NSMakeRange(range.location + 1, lastRange.location - range.location - 1)] integerValue]; //第二段数字

NSInteger thirdAppNum = [[appstoreVer substringWithRange:NSMakeRange(lastRange.location + 1, appstoreVer.length - lastRange.location - 1)] integerValue]; //第三段数字

NSRange rangeInfo = [infoVersion rangeOfString:@"."];

NSRange lastRangeInfo = [infoVersion rangeOfString:@"." options:NSBackwardsSearch];

NSInteger firstInfoNum = [[infoVersion substringWithRange:NSMakeRange(0, rangeInfo.location)] integerValue];

NSInteger secInfoNum = [[infoVersion substringWithRange:NSMakeRange(rangeInfo.location + 1, lastRangeInfo.location - rangeInfo.location - 1)] integerValue];

NSInteger thirdInfoNum = [[infoVersion substringWithRange:NSMakeRange(lastRangeInfo.location + 1, infoVersion.length - lastRangeInfo.location - 1)] integerValue];

NSLog(@"first = %zd,sec = %zd, last = %zd",firstAppNum,secAppNum,thirdAppNum);


三, 如何在项目中进行弹窗显示:

直接上代码了: 在appdelegate的方法- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 中添加如下:

__weak __typeof(self)weakSelf = self;

[YLDLoginManager getVersonForAppCompate:^(BOOL isSuccess,NSString *version, BOOL isNew) {

if (isSuccess) {

if (isNew) {

weakSelf.isNew = YES;

[[[UIAlertView alloc] initWithTitle:@"新版本更新"

message:[NSString stringWithFormat:@"新版本%@已发布, 请进行更新",version]

delegate:self

cancelButtonTitle:@"取消"

otherButtonTitles:@"确定", nil] show];}}}];

另外调用代理方法:

#pragma mark - UIAlertViewDelegate

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

switch (buttonIndex) {

case 0:{ //取消

}break;

case 1:{    //确定跳转App store

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"此处的url就是App Store上app复制链接的地址"]];

}break;

default:

break;

}}

注: 这里之所以需要认真判断新版本要比低版本大, 而不是仅仅根据两个版本字符串不同就提示版本有更新的原因:

1.0, 苹果官方审核时, 如果APP直接有弹窗提示需要下载, 不管你要跳转到哪里; 都会审核被拒;

2.0, 判断大小的方式来做, 在审核的时候, 苹果审核的APP是最新的, 版本号比App Store里的版本高, 那么就不会提示有弹窗; 但只要APP通过审核上了App Store, 以前老用户的版本都比App Store里的版本低, 就会出现弹窗;


if (firstAppNum > firstInfoNum) { //第一个数

return YES;

}else if(firstAppNum == firstInfoNum){

if (secAppNum > secInfoNum) { //第二个数

return YES;

}else if(secAppNum == secInfoNum){

if (thirdAppNum > thirdInfoNum) { //第三个数

return YES;

}else{

return NO;

}

}else{

return NO;

}

}else{

return NO;

}

你可能感兴趣的:(获取App Store中版本号,有更新则提示弹窗)