iOS动态加载自行构建的Framework[未完成]

1.新建FW.xcworkspace,FWFramework.xcodeproj,FWApp.xcodeproj。

iOS动态加载自行构建的Framework[未完成]_第1张图片
1

2.关联项目,将FWApp.xcodeproj和FWFramework.xcodeproj拖入FW.xcworkspace。

iOS动态加载自行构建的Framework[未完成]_第2张图片
2

3.在FWApp的Build Scheme中添加FWFramework的编译并前置。

iOS动态加载自行构建的Framework[未完成]_第3张图片
Paste_Image.png

4.在FWFramework中设置脚本,将编译好的FWFramework.framework放置在指定文件夹,并在FWApp中设置文件夹引用。

在指定位置新建文件夹


iOS动态加载自行构建的Framework[未完成]_第4张图片
4.2

在FWApp中拖入Embedded文件夹,设置为文件夹引用,减小依赖

iOS动态加载自行构建的Framework[未完成]_第5张图片
4.3

设置FWFramework脚本,将编译好的FWFramework.framework放在指定文件夹里


iOS动态加载自行构建的Framework[未完成]_第6张图片
4.1
# copy framework to FWApp/Embedded

rm -rf "${PROJECT_DIR}/../Embedded/${PROJECT_NAME}.framework"
cd "${CONFIGURATION_BUILD_DIR}"
cp -r "${PROJECT_NAME}.framework" "${PROJECT_DIR}/../Embedded/"

#end

尝试编译,在指定文件夹中预期,出现FWFramework.framework

iOS动态加载自行构建的Framework[未完成]_第7张图片
Paste_Image.png

5.在FWApp中调用FWFramework.framework

iOS动态加载自行构建的Framework[未完成]_第8张图片
Paste_Image.png
- (IBAction)clickGoFWFrameworkViewControllerButton:(id)sender {
    UIViewController *fwFrameworkViewController = [self loadFrameworkViewControllerWithBundleNamed:@"FWFramework" ViewControllerName:@"FWFrameworkViewController"];
    [self.navigationController pushViewController:fwFrameworkViewController animated:YES];
}

- (id)loadFrameworkViewControllerWithBundleNamed:(NSString *)frameworkName ViewControllerName:(NSString *)viewControllerName{
    NSFileManager *manager = [NSFileManager defaultManager];
    
    NSString *bundlePath = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"Embedded/%@",frameworkName] ofType:@"framework"];
    
    // Check bundle exists
    if (![manager fileExistsAtPath:bundlePath]) {
        UIAlertController *alertViewController = [UIAlertController alertControllerWithTitle:nil message:@"Not Found Framework" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"" style:UIAlertActionStyleDefault handler:nil];
        [alertViewController addAction:okAction];
        [self presentViewController:alertViewController animated:YES completion:nil];
        return nil;
    }
    
    // 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 : %@",error);
        return nil;
    }
    
    // Load class
    Class class = NSClassFromString(viewControllerName);
    if (!class) {
        NSLog(@"Unable to load class");
        return nil;
    }
    
    id object = [class new];
    return object;
}

RUN 调用成功(暂时还未解决加载nib和storyboard)

iOS动态加载自行构建的Framework[未完成]_第9张图片
Paste_Image.png

你可能感兴趣的:(iOS动态加载自行构建的Framework[未完成])