Swift Framework

Framework Project

From XCode 6, Framework project is added. Before it, we need to create Framework by ourselves
Compare to static library

Universal Framework

By default, Framework becomes one platform Simulator(i386, x86_64) or Device(arm)
To use both at the same time, we need to merge these platform result

How to

Steps

  • Build Simulator
  • Build
  • Add Aggregate target to your project
  • Add run script

Screen Shot 2015-12-07 at 4.58.40 PM

Screen Shot 2015-12-07 at 4.58.34 PM

Release or Debug are not matter

Screen Shot 2015-12-07 at 5.02.17 PM

Screen Shot 2015-12-07 at 5.01.02 PM

Script

UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal
FRAMEWORK_NAME=SDKTechSample

# make sure the output directory exists
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"

# 1. Copy the iphone device's framework structure to the universal folder
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${FRAMEWORK_NAME}.framework" "${UNIVERSAL_OUTPUTFOLDER}/"

# 2. Copy the Modules content from simulator's framework to universal folder
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${FRAMEWORK_NAME}.framework/Modules/${FRAMEWORK_NAME}.swiftmodule/x86_64.swiftmodule" "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_NAME}.framework/Modules/${FRAMEWORK_NAME}.swiftmodule/"


# 3. Create universal binary file using lipo and place the combined executable in the copied framework directory
lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}"

To run script, your universal-framework is build under your simulator and device framework directory

Header

By default, header is created by XCode.
Open it, and you can find this is like Objective-C.
Yeah, it’s objective-C integration material.
Actually, we can call swift class from Objective-C(need a bit technical)