如題, 今天我來給大家介紹:
一, 如何從App Store獲取自己APP的版本號(hào);
二, 如何判斷本地版本與App Store上的版本大小;
三, 如何在項(xiàng)目中進(jìn)行彈窗顯示;
現(xiàn)在開始講解:
一, 如何從App Store獲取自己APP的版本號(hào):
1. 獲取本地版本號(hào):
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
2, 獲取App Store上版本號(hào):
/* -----------? id為App Store內(nèi)app的id? -----------*/
2.1, 如何獲取自己app的id:
去App Store搜索自己的app --> 復(fù)制鏈接 -->隨便找塊地粘貼鏈接, 上面有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內(nèi)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(@"當(dāng)前本地版本為:%@, App Store版本:%@,是否有更新:%zd", version,ver,isLarge);
}else{
compate(NO,version,NO);
}
} failure:^(NSError * _Nonnull error) {
compate(NO,version,NO);
NSLog(@"error == %@",error);
}];
}
二, 如何判斷本地版本與App Store上的版本大小;
如下: 我只復(fù)制提供有兩個(gè)小數(shù)點(diǎn)的版本判斷, 取出了第一段,第二段,第三段數(shù)字進(jìn)行判斷大小; 每一段不限制長度;
如果,你的版本有三個(gè)及以上的小數(shù)點(diǎn): 則你需要NSRange截取的時(shí)候options:NSBackwardsSearch 這里可以選擇查找的起始地點(diǎn): 這里我不做詳細(xì)解說了, 自己去查找NSRange的截取方法, 截出你想要的結(jié)果
#pragma mark - 判斷版本是否最新 前者Appstore版本? 后者本地版本 注:僅支持(兩個(gè)點(diǎn)號(hào)的版本字符串) xxxx.xxxx.xxx
+(BOOL)checkVersionForAppstoreVer:(NSString *)appstoreVer andInfoVersion:(NSString *)infoVersion{
NSRange range = [appstoreVer rangeOfString:@"."]; //第一個(gè)點(diǎn)的位置
NSRange lastRange = [appstoreVer rangeOfString:@"." options:NSBackwardsSearch]; //第二個(gè)點(diǎn)的位置
NSInteger firstAppNum = [[appstoreVer substringWithRange:NSMakeRange(0, range.location)] integerValue]; //第一段數(shù)字
NSInteger secAppNum = [[appstoreVer substringWithRange:NSMakeRange(range.location + 1, lastRange.location - range.location - 1)] integerValue]; //第二段數(shù)字
NSInteger thirdAppNum = [[appstoreVer substringWithRange:NSMakeRange(lastRange.location + 1, appstoreVer.length - lastRange.location - 1)] integerValue]; //第三段數(shù)字
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);
三, 如何在項(xiàng)目中進(jìn)行彈窗顯示:
直接上代碼了: 在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:@"新版本%@已發(fā)布, 請進(jìn)行更新",version]
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"確定", nil] show];}}}];
另外調(diào)用代理方法:
#pragma mark - UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
switch (buttonIndex) {
case 0:{ //取消
}break;
case 1:{? ? //確定跳轉(zhuǎn)App store
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"此處的url就是App Store上app復(fù)制鏈接的地址"]];
}break;
default:
break;
}}
注: 這里之所以需要認(rèn)真判斷新版本要比低版本大, 而不是僅僅根據(jù)兩個(gè)版本字符串不同就提示版本有更新的原因:
1.0, 蘋果官方審核時(shí), 如果APP直接有彈窗提示需要下載, 不管你要跳轉(zhuǎn)到哪里; 都會(huì)審核被拒;
2.0, 判斷大小的方式來做, 在審核的時(shí)候, 蘋果審核的APP是最新的, 版本號(hào)比App Store里的版本高, 那么就不會(huì)提示有彈窗; 但只要APP通過審核上了App Store, 以前老用戶的版本都比App Store里的版本低, 就會(huì)出現(xiàn)彈窗;
if (firstAppNum > firstInfoNum) { //第一個(gè)數(shù)
return YES;
}else if(firstAppNum == firstInfoNum){
if (secAppNum > secInfoNum) { //第二個(gè)數(shù)
return YES;
}else if(secAppNum == secInfoNum){
if (thirdAppNum > thirdInfoNum) { //第三個(gè)數(shù)
return YES;
}else{
return NO;
}
}else{
return NO;
}
}else{
return NO;
}