万能的老板想把产品做成插件式,让更多的人能参与到产品的研发,又不让外人看到源码,所以默默的学习怎么才能封装一个真机和模拟器一起使用的静态库,再此分享下学习的成果,话不多说:
一、新建静态库工程
首先简历一个静态库文件:
接下来给你的静态库命名(在此博主以MyFirstStaticLib作为例子):
二、功能实现和添加
接下来在.h文件中写出需要暴露出给外部使用的接口或者参数:
#import
@interfaceMyFirstStaticLib : NSObject
- (NSInteger)addNumA:(NSInteger)AandNumB:(NSInteger)B;
@end
再.m文件中实现功能:
#import "MyFirstStaticLib.h"
@implementationMyFirstStaticLib
- (NSInteger)addNumA:(NSInteger)AandNumB:(NSInteger)B{
returnA+B;
}
@end
三、给所建立的静态库暴露头文件
将需要暴露的头文件加入Public中:
修改共有文件(需要暴露的文件)的路径(改为:include/$(PROJECT_NAME)):
四、编译:CMD+B
此时可以发现libMyFirstStaticLib.a文件由红变黑,证明编译成功。
由于默认的是Debug模式,所以编译出来的是模拟器使用的版本,所以在libMyFirstStaticLib.a路径下只发现有个Debug-iphoneos文件夹:
为了让真机可以使用我们的静态库,所以需要更改编译模式为Release:
再次编译CMD+B,发现目录下多出文件Release-iphoneos
五、建造一个Framework
得到新的标签:
修改标签名为Build Framework:
修改此标签中得脚本文件内容:
代码为:
set -e
exportFRAMEWORK_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"
再编译CMD+B。
六、整合模拟器版本和真机版本。
添加Target:aggregate07
增加依赖项:
增加脚本文件:
更改脚本命和脚本内容:
脚本命:MultiPlatform Build
脚本内容:
set -e
# If we're already inside this script then die
if[ -n"$RW_MULTIPLATFORM_BUILD_IN_PROGRESS"]; then
exit 0
fi
exportRW_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"
functionbuild_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
}
functionmake_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"
选中我们的Target:Framework,再次编译CMD+B,会发现桌面上已经出现我们需要得静态库文件:MyFirstStaticLib.framework
将此文件放入任意工程即可使用。