.a库是静态库,framework可以是动态库也可以是静态库,但是我们编译动态库在上架的时候是会被拒的。.a库是二进制的东西,而framework是除了二进制的东西还有资源文件,.a库要配合.h文件配合使用,framework则不需要。进入正题。
创建项目

放入功能代码文件


设置项目




在公开头文件的地方,.a库是需要我们自己去设置的,但是framework已经有默认设置了

然后调至如下图

接着只需把#import "UIColor+Category.h"
放到FrameworkDemo.h就行了(这里纠正一下,当前时间 2021-12-10,Project中不需要放入m文件,其次,#import 头文件应该放置在最后,下图位置并不正确,其实注释的绿色英文应该交代清楚了,再次,framework使用category,如果导入后发现报错,方法找不到之类的问题,需要在项目setting中的Other Linker Flags 中添加-ObjC)

生成framework
这里做法和.a生成一样


command + B
生成一个debug调试用的
然后再选择

注意这里

command + B


然后将这个两个文件夹复制到一个新建文件夹,在另外创建一个newFile文件夹(用来装合并两个framework)

合并framework
使用命令`lipo -create xxxxx/name.framework/name xxxxx/name2.framework/name2 -output xxxx/name3
lipo -create /Users/chenhaige/Desktop/aFile/framework/Debug-iphonesimulator/FrameworkDemo.framework/FrameworkDemo /Users/chenhaige/Desktop/aFile/framework/Release-iphoneos/FrameworkDemo.framework/FrameworkDemo -output /Users/chenhaige/Desktop/aFile/framework/newFile/FrameworkDemo
这时候会生成一个新的FrameworkDemo文件,但不是framework,接着我们复制一个framework,然后用新的FrameworkDemo替换掉原来的FrameworkDemo。

查看替换后的framework,支持设备有哪些,
lipo -info xxxx/name.framework/name

使用framework
直接拖进工程

然后引入头文件
#import <FrameworkDemo/FrameworkDemo.h>
实现方法
self.view.backgroundColor = [UIColor colorWithHexString:@"#333aaa" alpha:1];

另外
如果想通过cocoapods集成,可以查看另一篇相关文章,https://www.jianshu.com/p/845f2f60b221
或
http://www.cyhai.club/?p=185
但要注意的是
framework的引入使用
spec.vendored_frameworks = "xxxx/name.framework"
结束
(当前时间2021-12-10)补充:
制作通用的Framework脚本


# Merge Script
# 1
# Set bash script to exit immediately if any commands fail.
set -e
# 2
# Setup some constants for use later on.
FRAMEWORK_NAME= "你的framework名称"
# 3
# If remnants from a previous build exist, delete them.
if [ -d "${SRCROOT}/build" ]; then
rm -rf "${SRCROOT}/build"
fi
# 4
# Build the framework for device and for simulator (using
# all needed architectures).
xcodebuild -target "${FRAMEWORK_NAME}" -configuration Release -arch arm64 -arch armv7 -arch armv7s only_active_arch=no defines_module=yes -sdk "iphoneos"
xcodebuild -target "${FRAMEWORK_NAME}" -configuration Release -arch x86_64 -arch i386 only_active_arch=no defines_module=yes -sdk "iphonesimulator"
# 5
# Remove .framework file if exists on Desktop from previous run.
if [ -d "${HOME}/Desktop/${FRAMEWORK_NAME}.framework" ]; then
rm -rf "${HOME}/Desktop/${FRAMEWORK_NAME}.framework"
fi
# 6
# Copy the device version of framework to Desktop.
cp -r "${SRCROOT}/build/Release-iphoneos/${FRAMEWORK_NAME}.framework" "${HOME}/Desktop/${FRAMEWORK_NAME}.framework"
# 7
# Replace the framework executable within the framework with
# a new version created by merging the device and simulator
# frameworks' executables with lipo.
lipo -create -output "${HOME}/Desktop/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}" "${SRCROOT}/build/Release-iphoneos/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}" "${SRCROOT}/build/Release-iphonesimulator/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}"
# 8
# Copy the Swift module mappings for the simulator into the
# framework. The device mappings already exist from step 6.
cp -r "${SRCROOT}/build/Release-iphonesimulator/${FRAMEWORK_NAME}.framework/Modules/${FRAMEWORK_NAME}.swiftmodule/" "${HOME}/Desktop/${FRAMEWORK_NAME}.framework/Modules/${FRAMEWORK_NAME}.swiftmodule"
# 9
# Delete the most recent build.
if [ -d "${SRCROOT}/build" ]; then
rm -rf "${SRCROOT}/build"
fi
注意脚本要修改framework的名称,脚本lipo -create -output 输出的是桌面,所以,跑完后会在桌面看到一个framework,还值得注意的是,第8个步骤,如果你用的是OC,不存在与Swift的混合.swiftmodule是不存在,也不需要,请把第8个步骤注释掉
当然,也可以手动操作build完再合并
但是但是,苹果又搞事情了,xcode新版本,framework,合并之后会出现 Building for iOS, but the linked and embedded framework ‘xxx.framework’ was built for iOS + iOS Simulator.
因为构建方式改变,这种方式合并,并不能通用,网上给出了有修改构建方式为旧版本方式,另一种是修改setting里面的Validate WorkSpace 设置为 YES。但是苹果给出的答案是,使用新的framework打包方式,XCFramewrok,
XCFramewrok
set -e
FRAMEWORK_NAME="FrameWork名字"
IOS_SCHEME_NAME="Scheme名字"
if [ -d "${SRCROOT}/build" ]; then
rm -rf "${SRCROOT}/build"
fi
SIMULATOR_ARCHIVE_PATH="${SRCROOT}/build/${FRAMEWORK_NAME}-iphonesimulator.xcarchive"
DEVICE_ARCHIVE_PATH="${SRCROOT}/build/${FRAMEWORK_NAME}-iphoneos.xcarchive"
OUTPUT_DIR="${SRCROOT}/framework_out_universal/"
# Simulator xcarchieve
xcodebuild archive \
-scheme ${IOS_SCHEME_NAME} \
-archivePath ${SIMULATOR_ARCHIVE_PATH} \
-configuration Release \
-sdk iphonesimulator \
SKIP_INSTALL=NO
# Device xcarchieve
xcodebuild archive \
-scheme ${IOS_SCHEME_NAME} \
-archivePath ${DEVICE_ARCHIVE_PATH} \
-sdk iphoneos \
-configuration Release \
SKIP_INSTALL=NO
# Clean up old output directory
rm -rf "${OUTPUT_DIR}"
# Create xcframwork combine of all frameworks
xcodebuild -create-xcframework \
-framework ${SIMULATOR_ARCHIVE_PATH}/Products/Library/Frameworks/${FRAMEWORK_NAME}.framework \
-framework ${DEVICE_ARCHIVE_PATH}/Products/Library/Frameworks/${FRAMEWORK_NAME}.framework \
-output ${OUTPUT_DIR}/${FRAMEWORK_NAME}.xcframework
# Delete the most recent build.
if [ -d "${SRCROOT}/build" ]; then
rm -rf "${SRCROOT}/build"
fi
注意了,这里构建过来的路径是你的Framework项目本身文件夹下,别再桌面找了。
当然也可以手动处理
xcodebuild -create-xcframework -framework /Users/youthmba/Desktop/FrameWork/debug/Debug-iphoneos/MyFrameWork.framework -framework /Users/youthmba/Desktop/FrameWork/debug/Debug-iphonesimulator/MyFrameWork.framework -output /Users/youthmba/Desktop/FrameWork/All/MyFrameWork.xcframework
如果出现,安装提示uninstall,应该是


使用Do Not Embed。
网友评论