新建framework项目
设置生成fat包,包括模拟器x86_64和arm64
Buliding Settings -> Architectures -> Build Active Architecture Only 设置为NO
设置打包环境,选择release
edit Scheme
-> run
-> Build configuration
设置为 Release
设置静态库
Buliding Settings -> Linking -> Dead Code Stripping 设置为NO
Buliding Settings -> Linking -> Mach-O Type 设置为Static Library
创建打包合并framework脚本
添加打包+合成脚本代码:
# Install dir will be the final output to the framework.
# The following line create it in the root folder of the current project.
FMK_NAME=${PROJECT_NAME}
INSTALL_DIR=${SRCROOT}/Products/${FMK_NAME}.framework# Working dir will be deleted after the framework creation.
WRK_DIR=build
DEVICE_DIR=${WRK_DIR}/Build/Products/Release-iphoneos/${FMK_NAME}.framework
SIMULATOR_DIR=${WRK_DIR}/Build/Products/Release-iphonesimulator/${FMK_NAME}.framework# -configuration ${CONFIGURATION}
# Clean and Building both architectures.
xcodebuild -project ${FMK_NAME}.xcodeproj -scheme ${FMK_NAME} -configuration Release -derivedDataPath ${WRK_DIR} cleanxcodebuild -project ${FMK_NAME}.xcodeproj -scheme ${FMK_NAME} -configuration Release -derivedDataPath ${WRK_DIR} -sdk iphoneos -arch arm64 clean build
# simulator
xcodebuild -project ${FMK_NAME}.xcodeproj -scheme ${FMK_NAME} -configuration Release -derivedDataPath ${WRK_DIR} -sdk iphonesimulator -arch x86_64 build# Cleaning the oldest.
if [ -d "${INSTALL_DIR}" ]
then
rm -rf "${INSTALL_DIR}"
fimkdir -p "${INSTALL_DIR}"cp -R "${DEVICE_DIR}/" "${INSTALL_DIR}/"# Uses the Lipo Tool to merge both binary files (i386 + armv6/armv7) into one Universal final product.
lipo -create "${DEVICE_DIR}/${FMK_NAME}" "${SIMULATOR_DIR}/${FMK_NAME}" -output "${INSTALL_DIR}/${FMK_NAME}"cp -R "${SIMULATOR_DIR}/Modules/${FMK_NAME}.swiftmodule" "${INSTALL_DIR}/Modules/"#rm -r "${WRK_DIR}"open "${SRCROOT}/Products/"
运行脚本,生成的framework会在项目根目录的Products文件夹下。
添加swift类到工程
TestTool.swift
import UIKit// 可以在模块外被调用
public class TestTool: NSObject {// 只能在模块中被调用func HelloWorld(withParam param: String) -> Void {print(param)}// 可以在模块外被调用,只能在swift中被调用public func publicSayHelloWorld() {print("sayHelloWorld")}open func openSayHelloWorld() {print("sayHelloWorld")}// @objc public 同时修饰,才能在OC项目中被调用;并且也能被swift项目调用@objc public func sayHelloWorldToOC() {print("sayHelloWorldToOC")}
}
注意添加public修饰符到class,将类暴露出来
右键生成的framework,选择显示包内容,打开Headers目录下的XXXX-Swift.h,能找到刚刚添加的TestTool。
SWIFT_CLASS("_TtC7TestSDK8TestTool")
@interface TestTool : NSObject
- (void)sayHelloWorldToOC;
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end