公司最近需要将自己的蓝牙功能封装成SDK,就开始在网上搜索SDK的制作。
1.开始创建SDK
2.将开发环境选择8.0
选择Target->Build Setting -> 搜索mach
在Mach-O Type 选择要创建的framework 类型 静态库为Static Library
3.编写代码
你可以创建新的代码,也可以将别的工程的代码直接移过来。
注意:
swift 不像OC可以暴露接口,在swift中 要想给别的工程调用接口,记得在类,方法或属性前加public。public属于swift权限控制符。
swift权限控制符:
open 权限最大,可以被外界模块访问,继承重写
public 可以被外界工程访问
internal 默认文件创建时的权限,可以在本工程的访问
private 只可以在创建的文件内访问
如果该framework要求可以在OC内使用,记得在要使用的方法或属性前加@objc ,要暴露的类要继承NSObject
- 打包framework
使用脚本打包
点击Target -> 选择"+"
选择Cross-platform -> 选择 Aggregate
image.png选择新建的Aggregate -> Build Phases -> "+" -> New Run Script Phase
image.png点开新建的Run Script ,将下面这段脚本拷贝进去
#!/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}" clean build
xcodebuild -target "${PROJECT_NAME}" -configuration ${CONFIGURATION} -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" 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}"
image.png
选择Scheme Aggregate -> 选择Generic iOS Device 编辑运行 ,运行完毕后会自动弹出文件夹
image.png如果此时的framework 没达到要求,可以删除 文件夹内的Build文件夹和framework 重新打包
image.png通过该脚本打出的包是支持所支持架构
可以通过终端指令查看
image.png
网友评论