iOS 创建Framework 项目及所遇到的坑
在开发中有时候会有一些功能是其他项目中也会用到的,所以为了便于维护和减少重复写代码,我们可以通过创建静态库的方式来管理代码,将生成的Framework 包直接拖入所需要的工程就可以调用相应方法。
首先是如何创建一个Framework项目,
1.在 Xcode 中,File -> New -> Project -> Framework & Library -> Cocoa Touch Framework 来创建项目。

2.添加所需要的代码

3.在 MintmedicalDiseaseGuidelinesKit.h 文件中添加公共代码的头文件:#import<MintmedicalDiseaseGuidelinesKit/MDGDiseaseGuidelMainViewController.h>

4.开放公用代码的头文件。把需要曝光给外部的头文件放到 Targets -> Build Phases -> Headers -> Public 栏中。

5.配置参数,这里制作的是静态库,需要将Build Settings ->Linking中的Mach-O Type设置为Static Library。

6.打出通用包
和.a类似,生成的静态库需要进行合并才能适应不同的平台,为了方便,我们可以向Xcode添加脚本来解决这个繁琐的问题。
6.1新建target,选择other中的Aggregate,命名一般为framework名-Universal

6.2我们在target的Build Phases中点击加号,添加一个Run Script

6.3插入脚本

对应的脚本内容:
#!/bin/sh
UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal
# make sure the output directory exists
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"
# Step 1. Build Device and Simulator versions
xcodebuild -target "${PROJECT_NAME}" ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" -arch armv7 -arch armv7s -arch arm64 clean build
xcodebuild -target "${PROJECT_NAME}" -configuration ${CONFIGURATION} -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" -arch x86_64 -arch i386 clean build
# Step 2. Copy the framework structure (from iphoneos build) to the universal folder
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework" "${UNIVERSAL_OUTPUTFOLDER}/"
# Step 3. Copy Swift modules from iphonesimulator build (if it exists) to the copied framework directory
SIMULATOR_SWIFT_MODULES_DIR="${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule/."
if [ -d "${SIMULATOR_SWIFT_MODULES_DIR}" ]; then
cp -R "${SIMULATOR_SWIFT_MODULES_DIR}" "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule"
fi
# Step 4. Create universal binary file using lipo and place the combined executable in the copied framework directory
lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework/${PROJECT_NAME}"
# Step 5. Convenience step to copy the framework to the project's directory
cp -R "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework" "${PROJECT_DIR}"
# Step 6. Convenience step to open the project's directory in Finder
open "${PROJECT_DIR}"
6.4在 Targets -> MintmedicalDiseaseGuidelinesKit-Universal -> Target Dependencies 中添加 MintmedicalDiseaseGuidelinesKit 依赖项,这样使得在编译 MintmedicalDiseaseGuidelinesKit-Universal 的时候会先走正常流程编译 MintmedicalDiseaseGuidelinesKit.framework。

6.5编译MintmedicalDiseaseGuidelinesKit-Universal生成通用framework包,拖入其他工程就可以使用。


7.遇到的坑!!!
7.1.因为Framework是要给其他工程用的,所以新建文件的时候一定要加前缀,不管是不是暴露给外部的,统统都加前缀,这个坑了我好久,开始只给新建的文件加了前缀,还有些文件是原来工程里的,直接拉过来了,想想不暴露出去不改名应该没事,结果最后打出来的包放到其他工程中(存在同名文件的工程),一直编译不通过,搞了好久才发现是这个问题。
7.2.关于Framework中用到的资源文件(图片等)
在Framework中所用到的图片不能直接打包进Framework中,而是要单独打包成bundle,然后跟最终打包好的Framework一起放入目标工程中即可。

创建资源bundle的方法:
新建一个文件夹,把所需要的资源文件(这里是图片),放进去,然后直接修改它的名称和扩展名就可以了

调用bundle中的图片方法:
写了个image的分类方法,调用起来方便一点
#import "UIImage+EX.h"
@implementation UIImage (EX)
+ (UIImage *)getImageInBundleWithName:(NSString *)name {
NSBundle *libBundle = [NSBundle bundleWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"MDGImage.bundle"]];
NSString *path = @"";
if (libBundle) {
path = [[libBundle resourcePath] stringByAppendingPathComponent:name];
path = [path stringByAppendingString:@".png"];
}
return [UIImage imageWithContentsOfFile:path];
}
@end
调用方法:
[UIImage getImageInBundleWithName:@"backArrow"]
7.3 制作成动态库
错误描述:在使用静态库时,运行报错(Reason: Image Not Found)
具体原因:可能由于没有设置Mach-O Type,做的是动态库,在使用的时候需要额外加一个步骤,要把Framework同时添加到General --> Embedded Binaries中。
解决方案:将Mach-O Type设置为Static Library