Create Swift Cocoa Touch Framework
Let’s work on custom swift framework.
Step 1:
Select Xcode -> File -> New -> Project -> Cocoa Touch Framework
Give any name. Like: ASwiftFramework.
Step : 2
Select Xcode -> File -> New -> File -> Cocoa Touch Class
Give any name. Here we keep same name as ASwiftFramework.
Step 3:
Create some Public class func.
Now, we have to test this function.
Step 4:
Select Xcode -> File -> New -> Target -> Single view app.
Give any name.Like : TestSwiftFramework
Step 5:
Import ASwiftFramwork in Viewcontroller Class.
Class The framework method.
Step 6:
Select Project [TestSwiftFramework] Target.
Goto General Section -
Add ASwiftFramework at Embedded Binaries. It will automatically be added in Linked Frameworks and Libraries.
Step 7:
Goto Build Phases Section -
Add ASwiftFramework at Target Dependencies.
Step 8:
Select Xcode -> File -> New -> Target -> Cross-Platform -> Aggregate.
Give any name.Like: Universal
Step 9:
Add a New Run Script Phase
Paste the below Script in Run Script Section:
#!/bin/sh
UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal
# make sure the output directory exists
mkdir -p “${UNIVERSAL_OUTPUTFOLDER}”
# Step 1. Build Device and Simulator versions
xcodebuild -target “${PROJECT_NAME}” ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos BUILD_DIR=”${BUILD_DIR}” BUILD_ROOT=”${BUILD_ROOT}” clean build
xcodebuild -target “${PROJECT_NAME}” -configuration ${CONFIGURATION} -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO BUILD_DIR=”${BUILD_DIR}” BUILD_ROOT=”${BUILD_ROOT}” clean build
# Step 2. Copy the framework structure (from iphoneos build) to the universal folder
cp -R “${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework” “${UNIVERSAL_OUTPUTFOLDER}/”
# Step 3. Copy Swift modules (from iphonesimulator build) to the copied framework directory
cp -R “${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule/.” “${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule”
# Step 4. Create universal binary file using lipo and place the combined executable in the copied framework directory
lipo -create -output “${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/${PROJECT_NAME}” “${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework/${PROJECT_NAME}” “${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework/${PROJECT_NAME}”
# Step 5. Convenience step to copy the framework to the project’s directory
cp -R “${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework” “${PROJECT_DIR}”
# Step 6. Convenience step to open the project’s directory in Finder
open “${PROJECT_DIR}”
Step 10:
Add Aggregate [Universal] to Target Dependencies :
Step 11:
Select Universal Tagert and Archive :
It will open a folder like below:
It has created the universal framework, both for Simulator and device.
Use this framework in another app. Remember to import this framework, where to use and
Add ASwiftFramework at Embedded Binaries. It will automatically be added in Linked Frameworks and Libraries [as Step 6].