iOS Framework制作

创建工程

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 -e

export FRAMEWORK_LOCN="${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.framework"

# Create the path to the real Headers die
mkdir -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 die
if [ -n "$RW_MULTIPLATFORM_BUILD_IN_PROGRESS" ]; then
exit 0
fi
export RW_MULTIPLATFORM_BUILD_IN_PROGRESS=1

RW_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 name
if [[ "$SDK_NAME" =~ ([A-Za-z]+) ]]; then
RW_SDK_PLATFORM=${BASH_REMATCH[1]}
else
echo "Could not find platform name from SDK_NAME: $SDK_NAME"
exit 1
fi

# 2 - Extract the version from the SDK
if [[ "$SDK_NAME" =~ ([0-9]+.*$) ]]; then
RW_SDK_VERSION=${BASH_REMATCH[1]}
else
echo "Could not find sdk version from SDK_NAME: $SDK_NAME"
exit 1
fi

# 3 - Determine the other platform
if [ "$RW_SDK_PLATFORM" == "iphoneos" ]; then
RW_OTHER_PLATFORM=iphonesimulator
else
RW_OTHER_PLATFORM=iphoneos
fi

# 4 - Find the build directory
if [[ "$BUILT_PRODUCTS_DIR" =~ (.*)$RW_SDK_PLATFORM$ ]]; then
RW_OTHER_BUILT_PRODUCTS_DIR="${BASH_REMATCH[1]}${RW_OTHER_PLATFORM}"
else
echo "Could not find other platform build directory."
exit 1
fi
# 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_64
if [ "$RW_SDK_PLATFORM" == "iphonesimulator" ]; then
build_static_library "${SDK_NAME}"
fi

# Join the 2 static libs into 1 and push into the .framework
make_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 directories
cp -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 desktop
ditto "${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一下工程,确保编译成功

你可能感兴趣的:(iOS笔记,ios,framework)