更新後版本比較
目前app啟動之後都會進行版本比較,雖然apple稽核規則中明確指出自動檢測版本的app將會被拒絕,但是很多app種仍然保持著app版本的檢測,然後提供一個按鈕跳轉到app store去更新版本,具體做法應該是通過後臺介面來控制是否開啟檢測,稽核時關閉該功能,稽核通過後,開啟該功能。
鑑於NSString有提供compare的功能,但是會對於[@"10.1.1" compare:@"2.1.1"]
會出現問題,所以提供以下方法進行比較:
NSString+VersionCompare.h檔案
#import <Foundation/Foundation.h>
typedef enum : NSUInteger {
BiggerResultType,
SmallerResultType,
EqualResultType
} CompareResultType;
@interface NSString (VersionCompare)
#pragma mark - 與老版本比較
- (CompareResultType)versionCompare:(NSString *)oldVersion;
@end
複製程式碼
NSString+VersionCompare.m檔案
#import "NSString+VersionCompare.h"
@implementation NSString (VersionCompare)
#pragma mark - 與老版本比較
- (CompareResultType)versionCompare:(NSString *)oldVersion {
NSArray *currentVersionArray = [self componentsSeparatedByString:@"."];
NSArray *oldVersionArray = [oldVersion componentsSeparatedByString:@"."];
NSInteger currentVersionLength = currentVersionArray.count;
NSInteger oldVersionLength = oldVersionArray.count;
//小數點個數不相等,按下標索引從小到大比較,若較小的位數都相同,那麼較大位數的大(1.1.1 > 1.1)
if (currentVersionLength != oldVersionLength) {
NSInteger smallerLength = currentVersionLength < oldVersionLength ? currentVersionLength : oldVersionLength;
for (int i = 0; i < smallerLength; i++) {
if ([currentVersionArray[i] intValue] < [oldVersionArray[i] intValue]) {
return SmallerResultType;
} else if ([currentVersionArray[i] intValue] > [oldVersionArray[i] intValue]) {
return BiggerResultType;
} else {
continue; //相等就繼續
}
}
//前面的都相等, (1.1.1 > 1.1)
return currentVersionLength < oldVersionLength ? SmallerResultType : BiggerResultType;
} else {
//小數點個數相等,按下標索引從小到大比較
for (int i = 0; i < currentVersionLength; i++) {
if ([currentVersionArray[i] intValue] < [oldVersionArray[i] intValue]) {
return SmallerResultType;
} else if ([currentVersionArray[i] intValue] > [oldVersionArray[i] intValue]) {
return BiggerResultType;
} else {
continue; //相等就繼續
}
}
}
return EqualResultType;
}
@end
複製程式碼
使用方法:
//引入標頭檔案
#import "NSString+VersionCompare.h"
//使用
[@"1.1" versionCompare:@"1.1.1"]; //小
[@"10.2.0" versionCompare:@"2.2.0"]; //大
[@"1.2.0" versionCompare:@"1.2.1"]; //小
[@"1.1" versionCompare:@"1.1"]; //等
複製程式碼
新版本廣告展示判斷
目前很多app在版本更新的時候,都會有更新內容提醒,具體操作辦法如下:
//1.獲取快取的版本號
NSString *softwareVersion = [[NSUserDefaults standardUserDefaults] objectForKey:kSoftwareVersion];
//2.獲取當前的版本號
NSString *currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
if (![currentVersion isEqualToString:softwareVersion]) {//新版本更新
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:kSoftwareVersion];
[[NSUserDefaults standardUserDefaults] synchronize];
//可能要營銷的廣告檢視
}
複製程式碼
補充:
1.獲取系統版本號:
NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
複製程式碼
2.跳轉到app store詳情頁
NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/app/id你的appID"];
[[UIApplication sharedApplication] openURL:url];
複製程式碼