iOS動態加載自行構建的Framework[未完成]

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

1

2.關聯項目,將FWApp.xcodeproj和FWFramework.xcodeproj拖入FW.xcworkspace。

2

3.在FWApp的Build Scheme中添加FWFramework的編譯并前置。

Paste_Image.png

4.在FWFramework中設置腳本,將編譯好的FWFramework.framework放置在指定文件夾,并在FWApp中設置文件夾引用。

在指定位置新建文件夾


4.2

在FWApp中拖入Embedded文件夾,設置為文件夾引用,減小依賴

4.3

設置FWFramework腳本,將編譯好的FWFramework.framework放在指定文件夾里


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

Paste_Image.png

5.在FWApp中調用FWFramework.framework

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)

Paste_Image.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容