在项目逐渐成熟之后,需要app升级之后执行一些特定的操作,比如删除之前的无用缓存或更新一些配置等。
项目中可使用runtime+category来实现此操作
启动后检查升级类 XXAppUpdater
//启动后执行方法
[XXAppUpdater updateToVersion];
XXAppUpdater 具体实现
static NSArray* getVersionByStr(NSString *strVersion) {
NSArray *versionStrArr = [strVersion componentsSeparatedByString:@"."];
NSMutableArray *version = [NSMutableArray arrayWithCapacity:versionStrArr.count];
for (int i = 0; i < [versionStrArr count]; i++) {
NSString *str = [versionStrArr objectAtIndex:i];
[version addObject:[NSNumber numberWithInt:[str intValue]]];
}
return version;
}
static NSArray* getVersionByMethodName(NSString *methodName) {
NSError *error = NULL;
NSArray *arrRegex = @[@"appUpgradeTo_(\\d+)_(\\d+)_(\\d+)",
@"appUpgradeTo_(\\d+)_(\\d+)"];
for (NSString *regexStr in arrRegex) {
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr
options:NSRegularExpressionCaseInsensitive
error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:methodName
options:0
range:NSMakeRange(0, [methodName length])];
if (match) {
const NSUInteger count = match.numberOfRanges;
NSMutableArray *version = [NSMutableArray arrayWithCapacity:count];
for (int i = 1; i < count; i++) {
NSString *str = [methodName substringWithRange:[match rangeAtIndex:i]];
[version addObject:[NSNumber numberWithInt:[str intValue]]];
}
return version;
}
}
return nil;
}
static NSInteger compareVersion(NSArray *ver1, NSArray *ver2) {
const NSInteger count = MIN(ver1.count, ver2.count);
for (int i = 0; i < count; i++) {
NSNumber *left = [ver1 objectAtIndex:i];
NSNumber *right = [ver2 objectAtIndex:i];
if (left.intValue > right.intValue) {
return 1;
}
if (left.intValue < right.intValue) {
return -1;
}
}
if (ver1.count > ver2.count) {
return 1;
} else if (ver1.count < ver2.count) {
return -1;
} else {
return 0;
}
}
+ (void)updateToVersion {
unsigned int count;
//runtime 获取该类的所有方法
Method* methods = class_copyMethodList(object_getClass([XXAppUpdater class]), &count);
if (count == 0) {
[WJUserDefaults updateAppVersion];
return;
}
//有值的话去创建数组得到方法名
NSMutableArray* methodArray = [NSMutableArray arrayWithCapacity:count];
for (int i = 0; i < count; i++)
{
SEL selector = method_getName(methods[i]);
const char* methodName = sel_getName(selector);
[methodArray addObject:[NSString stringWithCString:methodName encoding:NSUTF8StringEncoding]];
}
free(methods);
//获取目前的版本号
NSString *version = [WJUserDefaults localAppVersion];
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
NSArray *left = getVersionByStr(version);
NSArray *right = getVersionByMethodName(evaluatedObject);
if (right == nil) {
return NO;
}
return compareVersion(left, right) < 0;
}];
//筛选出需要执行的方法
NSArray *upgradeMethods = [methodArray filteredArrayUsingPredicate:predicate];
WJUpdaterLog(@"upgradeMethods = %@", upgradeMethods);
upgradeMethods = [upgradeMethods sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSArray *left = getVersionByMethodName(obj1);
NSArray *right = getVersionByMethodName(obj2);
return compareVersion(left, right);
}];
//遍历去执行分类的升级后需要执行的方法
for (NSString *methodName in upgradeMethods) {
SEL upgrade = NSSelectorFromString(methodName);
if (![[self class] performSelector:upgrade]) {
WJUpdaterLog(@"%@ failed", methodName);
break;
} else {
WJUpdaterLog(@"%@ succeed", methodName);
}
}
[WJUserDefaults updateAppVersion];
}
每次升级需要更新的操作放在XXAppUpdater分类中即可
//添加分类方法
+ (BOOL)appUpgradeTo_8_8_8 {
//升级后执行的操作
}