Framework

作者: iVikings | 来源:发表于2022-12-20 17:54 被阅读0次

iOS 创建 Static Framework

目录

创建 Framework

1、打开 Xcode -> File -> New Project -> iOS -> Framework
Framework
2、 选择 TARGETS -> Build Settings -> Mach-O Type -> Static Library
Static Library
3、Code Signing Identity 的值清空
Code Signing Identity
4、选择 PROJECT -> Info -> 修改 iOS Deployment Target 为最低支持版本
5、若 Framework 中用到分类,需要设置 Other Linker Flags,添加 -ObjC
Other Linker Flags
6、创建对应的类文件
7、修改 TARGETS —> Build Phases

将需要呈现给来的头文件,直接从 Project 拖到 Public 中. 不想呈现出来的 .h文件不建议拖到Private 中,放在 Project 中即可.

8、Build Libraries for Distribution 设置成 YES

No 'swiftinterface' files found within '/Users/huipeng/Library/Developer/Xcode/DerivedData/WBLiveBox-dycshnykuupzuaaitrknlpgwikma/Build/Products/Release-iphoneos/WLiveUIKit.framework/Modules/WLiveUIKit.swiftmodule'.

添加 Framework 编译脚本

#!/bin/sh

#sh build_framework.sh -bRelease -v1.0.0.0

configuration="Release"
DEV_FLAG=""
VER_FLAG=""

for i in "$@"
do
PFLAG=`echo $i|cut -b1-2`
PPARAM=`echo $i|cut -b3-`
if [ $PFLAG == "-b" ]
then
DEV_FLAG=$PPARAM
elif [ $PFLAG == "-v" ]
then
VER_FLAG=$PPARAM
fi
done

if [[ ${DEV_FLAG} == "debug" ]] || [[ ${DEV_FLAG} == "Debug" ]]
then
configuration="Debug"
else
configuration="Release"
fi


PROJECT_WORKSPACE="../WBCloudVideo.xcworkspace"
PROJECT_SCHEME="WMeetingUIKit"
TARGET_NAME="WMeetingUIKit"
TARGET_DEVICE="iphoneos"
TARGET_SIMULATOR="iphonesimulator"

BUILD_PATH=$(xcodebuild -workspace ${PROJECT_WORKSPACE} -scheme ${PROJECT_SCHEME} -showBuildSettings | grep " BUILD_DIR")
BUILD_PATH=${BUILD_PATH##*= }
DEVICE_DIR=${BUILD_PATH}/${configuration}-iphoneos/${TARGET_NAME}.framework
SIMULATOR_DIR=${BUILD_PATH}/${configuration}-iphonesimulator/${TARGET_NAME}.framework

if [ -d ${BUILD_PATH} ]; then
rm -rf ${BUILD_PATH}
fi


# 修改编译时间和版本号
if [ ${VER_FLAG} ]; then
    sh ./build_version.sh -v${VER_FLAG}
fi


xcodebuild clean \
-workspace ${PROJECT_WORKSPACE} \
-scheme ${PROJECT_SCHEME} \
-configuration ${configuration} \
-sdk ${TARGET_SIMULATOR} \
-UseModernBuildSystem=YES

xcodebuild clean \
-workspace ${PROJECT_WORKSPACE} \
-scheme ${PROJECT_SCHEME} \
-configuration ${configuration} \
-sdk ${TARGET_DEVICE} \
-UseModernBuildSystem=YES


echo "***开始build iphonesimulator文件${configuration}***"

xcodebuild build \
-workspace ${PROJECT_WORKSPACE} \
-scheme ${PROJECT_SCHEME} \
-configuration ${configuration} \
-sdk ${TARGET_SIMULATOR} -arch x86_64 \
-UseModernBuildSystem=YES \
 ONLY_ACTIVE_ARCH=NO \
 GCC_GENERATE_DEBUGGING_SYMBOLS=NO \
 | xcpretty

echo "***开始build iphoneos文件***${configuration}"

xcodebuild build \
-workspace ${PROJECT_WORKSPACE} \
-scheme ${PROJECT_SCHEME} \
-configuration ${configuration} \
-sdk ${TARGET_DEVICE} -arch arm64 \
-UseModernBuildSystem=YES \
 ONLY_ACTIVE_ARCH=NO \
 GCC_GENERATE_DEBUGGING_SYMBOLS=YES \
 | xcpretty


# 自动移动到 Demo SDK 目录
DST_DIR="../Frameworks"
if [ ! -d $DST_DIR ]; then
mkdir -p "$DST_DIR"
fi
if [ -d ${DST_DIR}/${TARGET_NAME}.xcframework ]; then
rm -rf ${DST_DIR}/${TARGET_NAME}.xcframework
fi

xcodebuild -create-xcframework \
-framework ${DEVICE_DIR} \
-framework ${SIMULATOR_DIR} \
-output ${DST_DIR}/${TARGET_NAME}.xcframework

rm -r ${BUILD_PATH}
rm -r $(pwd)/Products

echo "***完成Build ${TARGET_NAME}静态库${configuration}****"

执行脚本:

sh -x build_framework.sh -bRelease -v1.4.2.0

<div id="FAQ"> 常见问题 </div>

Q:找不到头文件?
A:Build Settings 设置 Framework Search Paths 路径,如:

$(SRCROOT)/../Frameworks

Q:若打包出现如下错误:

ld: bitcode bundle could not be generated because 'xxx.framework/xxx(xxx_vers.o)' was built without full bitcode. All object files and libraries for bitcode must be generated from Xcode Archive or Install build file 'xxx.framework/xxx' for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ld: bitcode bundle could not be generated because 'xxx.framework/xxx(xxx_vers.o)' was built without full bitcode. All object files and libraries for bitcode must be generated from Xcode Archive or Install build file 'xxx.framework/xxx' for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: the following command failed with exit code 1 but produced no further output

A:打开 framework 静态工程,选择 TARGETS -> Build Settings -> 找到 Apple Clang - Custom Compiler Flags -> Other C Flags 添加 -fembed-bitcode

Q:编译报错

Include of non-modular header inside framework module 'xxx.class': '/xxx/Pods/Headers/Public/xxx/class.h'

A:进行如下修改:

TARGETS -> Build Settings -> Allow Non-modular Includes In Framework Modules -> Yes

相关文章

网友评论

      本文标题:Framework

      本文链接:https://www.haomeiwen.com/subject/cihpqdtx.html