iOS SDK Development Build XCFramework

Next is to make XCFramework for deploy.

By default build, can build only for simulator or device based on build setting at the top of XCode.

But for developers, import SDK and test with simulator and devices. SDK should work with both. To support both, need to prepare Universal Framework

スポンサーリンク

Add Build Target

To make Universal Framework, need to add script when build.

To support Run script, we can add TARGET.

Select Aggregate. After this, select Build Phases and press + button.

Select “New Run Script Phase” and add script.

Run Script

Add following script. I added 2 patterns. You can choose either.

The difference is to use function or not

Run Script 1

# Type a script or drag a script file from your workspace to insert its path.
# Set Properties Output Directory
OUTPUT_DIR=${PROJECT_DIR}/XCFramework
# Intermediate files
DERIVED_DIR=${OUTPUT_DIR}/${CONFIGURATION}-derived
# Archives
ARCHIVE_DIR=${OUTPUT_DIR}/${CONFIGURATION}-archive
# XCFramework
XCFRAMEWORK_DIR=${OUTPUT_DIR}/${CONFIGURATION}-xcframework
 
# Delete Output Directory first
rm -rf ${OUTPUT_DIR}
# Create Intermediate Directory
mkdir -p ${DERIVED_DIR}
# Create Archive Directory
mkdir -p ${ARCHIVE_DIR}
# Create xcframework Directory
mkdir -p ${XCFRAMEWORK_DIR}
 
# iOS Device Archive File Path
ARCHIVE_FILE_IOS=${ARCHIVE_DIR}/ios.xcarchive
echo "ARCHIVE_FILE_IOS:${ARCHIVE_FILE_IOS}"
# iOS Simulator Archive File Path
ARCHIVE_FILE_IOS_SIMULATOR=${ARCHIVE_DIR}/iossimulator.xcarchive
echo "ARCHIVE_FILE_IOS_SIMULATOR:${ARCHIVE_FILE_IOS_SIMULATOR}"
 
# Build iOS device Archive
xcodebuild archive -scheme ${PROJECT_NAME} -destination="iOS" -archivePath $ARCHIVE_FILE_IOS -derivedDataPath $DERIVED_DIR -sdk iphoneos SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES
 
# Build iOS simulator Archive
xcodebuild archive -scheme ${PROJECT_NAME} -destination="iOS Simulator" -archivePath $ARCHIVE_FILE_IOS_SIMULATOR -derivedDataPath $DERIVED_DIR -sdk iphonesimulator SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES
 
# Create xcframework
xcodebuild -create-xcframework -framework $ARCHIVE_FILE_IOS/Products/Library/Frameworks/${PROJECT_NAME}.framework -framework $ARCHIVE_FILE_IOS_SIMULATOR/Products/Library/Frameworks/${PROJECT_NAME}.framework -output $XCFRAMEWORK_DIR/${PROJECT_NAME}.xcframework
 
# Open Finder
open ${XCFRAMEWORK_DIR}

Run Script 2

# Type a script or drag a script file from your workspace to insert its path.
env > env.txt
#! /bin/sh -e
# Release dir path

OUTPUT_DIR_PATH="${PROJECT_DIR}/XCFramework"


function archivePathSimulator {
  local DIR=${OUTPUT_DIR_PATH}/archives/"${1}-SIMULATOR"
  echo "${DIR}"
}

function archivePathDevice {
  local DIR=${OUTPUT_DIR_PATH}/archives/"${1}-DEVICE"
  echo "${DIR}"
}

# Archive takes 3 params
#
# 1st == SCHEME
# 2nd == destination
# 3rd == archivePath
function archive {
    echo "▸ Starts archiving the scheme: ${1} ${CONFIGURATION} for destination: ${2};\n▸ Archive path: ${3}.xcarchive"
    xcodebuild clean archive \
    -project "${PROJECT_NAME}.xcodeproj" \
    -scheme ${1} \
    -configuration ${CONFIGURATION} \
    -destination "${2}" \
    -archivePath "${3}" \
    SKIP_INSTALL=NO \
    OBJROOT="${OBJROOT}/DependentBuilds" \
    BUILD_LIBRARY_FOR_DISTRIBUTION=YES | xcpretty
}

# Builds archive for iOS simulator & device
function buildArchive {
  SCHEME=${1}
  archive $SCHEME "generic/platform=iOS Simulator" $(archivePathSimulator $SCHEME)
  archive $SCHEME "generic/platform=iOS" $(archivePathDevice $SCHEME)
}

# Creates xc framework
function createXCFramework {
  FRAMEWORK_ARCHIVE_PATH_POSTFIX=".xcarchive/Products/Library/Frameworks"
  FRAMEWORK_SIMULATOR_DIR="$(archivePathSimulator $1)${FRAMEWORK_ARCHIVE_PATH_POSTFIX}"
  FRAMEWORK_DEVICE_DIR="$(archivePathDevice $1)${FRAMEWORK_ARCHIVE_PATH_POSTFIX}"
  xcodebuild -create-xcframework \
            -framework ${FRAMEWORK_SIMULATOR_DIR}/${1}.framework \
            -framework ${FRAMEWORK_DEVICE_DIR}/${1}.framework \
            -output ${OUTPUT_DIR_PATH}/xcframeworks/${1}.xcframework
}

echo "#####################"
echo "▸ Cleaning the dir: ${OUTPUT_DIR_PATH}"
rm -rf $OUTPUT_DIR_PATH



#### Dynamic Framework ####
DYNAMIC_FRAMEWORK="${PROJECT_NAME}"
echo "▸ Archive $DYNAMIC_FRAMEWORK"
buildArchive ${DYNAMIC_FRAMEWORK}
echo "▸ Create $DYNAMIC_FRAMEWORK.xcframework"
createXCFramework ${DYNAMIC_FRAMEWORK}

What those script did is to run xcodebuild with xcframework mode, build both real device and simulator and combine one xcframework.

XCFramework Structure check

Now, we can get iOSModuleSDK.framework.

This is a deploy target.

Before deploy, let’s check inside whether this is expected.

iOSModuleSDK.framework
 |-Info.plist
 |-ios-arm64
 |   |-iOSModuleSDK.framework
 |ios-arm64_x86_64-simulator
     |-iOSModuleSDK.framework

I would like to check both simulator and real device framework is inside, ios-arm64 is for realdevice, ios-arm64_x86_64 is for simulator.

From M1 chip, simulator is arm64 as well. So, Intel chip x86_64 is missing by default

But, for development, XCode seems to be required x86_64. (if missing, error has come)

iOS
スポンサーリンク
Professional Programmer2

コメント