课程咨询: 400-996-5531 / 投诉建议: 400-111-8989
认真做教育 专心促就业
烟台达内培训老师教大家怎么样用framework 的方式实现 app 的热修复。
由于 Apple 不希望开发者绕过 App Store 来更新 app,因此只有对于不需要上架的应用,才能以 framework 的方式实现 app 的更新。
但是理论上只要保持签名一致,在 dlopen 没有被禁止的情况下应该是行的通的。(因为没有去实践,只能这样 YY 了。)
但是不论是哪种方式都得保证 服务器上的 framework 与 app 的签名要保持一致。
实现大致思路
下载新版的 framework
先到 document 下寻找 framework。然后根据条件加载 bundle or document 里的 framework。
NSString *fileName = @"remote";
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = nil;
if ([paths count] != 0) {
documentDirectory = [paths objectAtIndex:0];
}
NSFileManager *manager = [NSFileManager defaultManager];
NSString *bundlePath = [[NSBundle mainBundle]
pathForResource:fileName ofType:@"framework"];
BOOL loadDocument = YES;
// Check if new bundle exists
if (![manager fileExistsAtPath:bundlePath] && loadDocument) {
bundlePath = [documentDirectory stringByAppendingPathComponent:[fileName stringByAppendingString:@".framework"]];
}
再加载 framework
// Load bundle
NSError *error = nil;
NSBundle *frameworkBundle = [NSBundle bundleWithPath:bundlePath];
if (frameworkBundle && [frameworkBundle loadAndReturnError:&error]) {
NSLog(@"Load framework successfully");
}else {
NSLog(@"Failed to load framework with err: %@",error);
}
加载类并做事情
// Load class
Class PublicAPIClass = NSClassFromString(@"PublicAPI");
if (!PublicAPIClass) {
NSLog(@"Unable to load class");
}
NSObject *publicAPIObject = [PublicAPIClass new];
[publicAPIObject performSelector:@selector(mainViewController)]