preface

In development, it is often necessary to package code into a Framework, and it is cumbersome to manually configure the merge each time. Here is a way to dynamically generate the Framework using scripts. Use scripts to dynamically follow the project code, ready to package, simple and convenient

1. Create a dedicated Target for the Framework and configure packaging

The first step

Create a Target in the project, select Framework, and use it to package Framework

The second step

Select the Target corresponding to package Framework, find Build Setting, search iOS, and modify the corresponding version

The third step

Find the Dead Code Stripping option and set it to NO, which is not necessary and is recommended

The fourth step

Change the Link With Standard Libraries option to NO. This operation is unnecessary and recommended

Step 5

Change the Mach-o Type option to static libraries. Non-essential operation, must be static library if you want to put store.

Step 6

Set the Build Active Architecture Only option to NO

Step 7

Configure the Architecture to support ARMV7s. This operation is not necessary. Check whether the Framework needs to support the corresponding Architecture model

Step 8

Set public. H header files, not public. H header files, under the Private or Project option. Pay attention to the Compile Sources option to see if there is a. M file corresponding to the header file. If there is no need to add it, otherwise the Framework will report an error

Step 9

Switch to the Framework Target. H file and import the exposed. H header file

2. Create the Shell script Target and add it to the package Framework script

The first step

Create a shell-specific Target

The second step

Add script Phase

The third step

Add the script. The target name in the script needs to be filled in according to the newly created Framework package Targrt

#! /bin/sh
The target name to build
TARGET_NAME="CLFramework"
if [[ The $1 ]]
then
TARGET_NAME=The $1
fi
UNIVERSAL_OUTPUT_FOLDER="${SRCROOT}/Products"

Create the output directory and delete the previous framework files
mkdir -p "${UNIVERSAL_OUTPUT_FOLDER}"
rm -rf "${UNIVERSAL_OUTPUT_FOLDER}/${TARGET_NAME}.framework"

Build the Framework for the simulator and the real machine separately
xcodebuild -target "${TARGET_NAME}" ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build
xcodebuild -target "${TARGET_NAME}" ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphonesimulator BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build

# copy framework to univer directory
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${TARGET_NAME}.framework" "${UNIVERSAL_OUTPUT_FOLDER}"

Export the final framework to the build directory
lipo -create -output "${UNIVERSAL_OUTPUT_FOLDER}/${TARGET_NAME}.framework/${TARGET_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${TARGET_NAME}.framework/${TARGET_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${TARGET_NAME}.framework/${TARGET_NAME}"

Delete extraneous configuration files generated after compilation
dir_path="${UNIVERSAL_OUTPUT_FOLDER}/${TARGET_NAME}.framework/"
for file in ls $dir_path
do
if [[ ${file}= ~".xcconfig" ]]
then
rm -f "${dir_path}/${file}"
fi
done
If the build folder exists, delete it
if [ -d "${SRCROOT}/build" ]
then
rm -rf "${SRCROOT}/build"
fi
rm -rf "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator" "${BUILD_DIR}/${CONFIGURATION}-iphoneos"
# Open the merged folder
open "${UNIVERSAL_OUTPUT_FOLDER}"
Copy the code

3. Use Shell to package the Framework

Select the Target corresponding to the Shell script, select general, run the code, when selecting the model, be sure to select general, so as to include the simulator and the real machine.

conclusion

After the package is complete, you can run the lip-info command to view the models supported by the packaged Framework. The Demo address is ——–>>CLFrameworkDemo