iOS Framework制作

来源:互联网 发布:淘宝买来的模板怎么装 编辑:程序博客网 时间:2024/06/10 20:24

创建工程

File->New->project->Cocoa Touch Static Library

然后要求你输入Product Name

Product Name 为你framework的名字,例如以后你的framework叫Example.framework,那么Product Name 就叫 Example

工程设置

选中TARGETS 中的Example(小房子图标)

切换到Build Settings,设置
- Dead Code Stripping -> NO
- Strip Debug Symbols During Copy -> NO
- Strip Style -> Non-Clobal Symbols

添加文件

然后切换到 Build Phases,点一下选项卡中的空白处

然后选择工具栏中的Editor->Add Build Phase->Add Copy Header Build Phase(如果是灰色的不能点击,那么点一下 Build Phases选项卡中的空白处就出来了)

这时候Build Phases 中会出现Headers 下拉列表

将你需要的.h和.m文件复制、添加到你的Framework工程

这时候你会发现Headers 中的Project 出现了你刚刚添加的.h文件

将想暴露出来的.h文件拉到Public

设置静态库脚本

Editor->Add Build Phase -> Add Run Script Build Phase
(如果是灰色的不能点击,那么点一下 Build Phases选项卡中的空白处就出来了)

双击标题Run Script ,重命名为 Build Framework

然后在文本框中粘贴以下脚本

set -eexport FRAMEWORK_LOCN="${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.framework"# Create the path to the real Headers diemkdir -p "${FRAMEWORK_LOCN}/Versions/A/Headers"# Create the required symlinks/bin/ln -sfh A "${FRAMEWORK_LOCN}/Versions/Current"/bin/ln -sfh Versions/Current/Headers "${FRAMEWORK_LOCN}/Headers"/bin/ln -sfh "Versions/Current/${PRODUCT_NAME}" \"${FRAMEWORK_LOCN}/${PRODUCT_NAME}"# Copy the public headers into the framework/bin/cp -a "${TARGET_BUILD_DIR}/${PUBLIC_HEADERS_FOLDER_PATH}/" \"${FRAMEWORK_LOCN}/Versions/A/Headers"

创建Aggregate

选中TARGETS ,点击+号,找到iOS->Other->Aggregate,点击Next,将目标命名为Framework

选中Framework,展开Target Dependencies,点击+号,选中Example

切换到Build Pheses 选项卡,点击Editor->Add Build Phase->Add Run Script Build Phase,创建一个新的Run Script Build Phase。双击Run Script,重命名脚本的名字,这次命名为MultiPlatform Build,粘贴Bash脚本

set -e# If we're already inside this script then dieif [ -n "$RW_MULTIPLATFORM_BUILD_IN_PROGRESS" ]; thenexit 0fiexport RW_MULTIPLATFORM_BUILD_IN_PROGRESS=1RW_FRAMEWORK_NAME=${PROJECT_NAME}RW_INPUT_STATIC_LIB="lib${PROJECT_NAME}.a"RW_FRAMEWORK_LOCATION="${BUILT_PRODUCTS_DIR}/${RW_FRAMEWORK_NAME}.framework"function build_static_library {    # Will rebuild the static library as specified    #     build_static_library sdk    xcrun xcodebuild -project "${PROJECT_FILE_PATH}" \    -target "${TARGET_NAME}" \    -configuration "${CONFIGURATION}" \    -sdk "${1}" \    ONLY_ACTIVE_ARCH=NO \    BUILD_DIR="${BUILD_DIR}" \    OBJROOT="${OBJROOT}" \    BUILD_ROOT="${BUILD_ROOT}" \    SYMROOT="${SYMROOT}" $ACTION}function make_fat_library {    # Will smash 2 static libs together    #     make_fat_library in1 in2 out    xcrun lipo -create "${1}" "${2}" -output "${3}"}# 1 - Extract the platform (iphoneos/iphonesimulator) from the SDK nameif [[ "$SDK_NAME" =~ ([A-Za-z]+) ]]; thenRW_SDK_PLATFORM=${BASH_REMATCH[1]}elseecho "Could not find platform name from SDK_NAME: $SDK_NAME"exit 1fi# 2 - Extract the version from the SDKif [[ "$SDK_NAME" =~ ([0-9]+.*$) ]]; thenRW_SDK_VERSION=${BASH_REMATCH[1]}elseecho "Could not find sdk version from SDK_NAME: $SDK_NAME"exit 1fi# 3 - Determine the other platformif [ "$RW_SDK_PLATFORM" == "iphoneos" ]; thenRW_OTHER_PLATFORM=iphonesimulatorelseRW_OTHER_PLATFORM=iphoneosfi# 4 - Find the build directoryif [[ "$BUILT_PRODUCTS_DIR" =~ (.*)$RW_SDK_PLATFORM$ ]]; thenRW_OTHER_BUILT_PRODUCTS_DIR="${BASH_REMATCH[1]}${RW_OTHER_PLATFORM}"elseecho "Could not find other platform build directory."exit 1fi# Build the other platform.build_static_library "${RW_OTHER_PLATFORM}${RW_SDK_VERSION}"# If we're currently building for iphonesimulator, then need to rebuild#   to ensure that we get both i386 and x86_64if [ "$RW_SDK_PLATFORM" == "iphonesimulator" ]; thenbuild_static_library "${SDK_NAME}"fi# Join the 2 static libs into 1 and push into the .frameworkmake_fat_library "${BUILT_PRODUCTS_DIR}/${RW_INPUT_STATIC_LIB}" \"${RW_OTHER_BUILT_PRODUCTS_DIR}/${RW_INPUT_STATIC_LIB}" \"${RW_FRAMEWORK_LOCATION}/Versions/A/${RW_FRAMEWORK_NAME}"# Ensure that the framework is present in both platform's build directoriescp -a "${RW_FRAMEWORK_LOCATION}/Versions/A/${RW_FRAMEWORK_NAME}" \"${RW_OTHER_BUILT_PRODUCTS_DIR}/${RW_FRAMEWORK_NAME}.framework/Versions/A/${RW_FRAMEWORK_NAME}"# Copy the framework to the user's desktopditto "${RW_FRAMEWORK_LOCATION}" "${HOME}/Desktop/${RW_FRAMEWORK_NAME}.framework"

编译

选择编译目标 Example -> Generic iOS Device,键盘command+B 编译

然后选择编译目标Framework -> Generic iOS Device,键盘command+B 编译

切换到桌面看下,你要的framework 是不是躺在桌面等你了

验证

验证framework架构是否正确,打开终端,cd到你的framework里面,执行命令

xcrun lipo -info Example

输出

Architectures in the fat file: Example are: armv7 i386 x86_64 arm64

恭喜您framework制作成功!

后记

如果有用到category,要在Build Setting 里面设置 Other Linker Flags -> -ObjC -all_load

如果要重新编译,记得先Clean一下工程,确保编译成功

原创粉丝点击