Search code examples
xcode8universalswift-custom-framework

Universal framework using Xcode 8.3?


I'm trying to build universal swift framework. When I completed developing framework following several tutorials such as

https://medium.com/swiftindia/build-a-custom-universal-framework-on-ios-swift-549c084de7c8

Creating a universal framework using Xcode 8?

Also by using Aggregate target as well but fail to run in Simulator.

Note: I developed using Swift 3, Xcode Version 8.3

I tried with tutorials scripts too but fail.

Also my framework Xcode extension is xcodeproj.

My question is How to develop universal swift framework which will be release to client where he can test through Simulator as well as with real device?

Thanks in advance.

Edit: I followed this answer instruction and successfully build swift framework.

https://stackoverflow.com/a/26691080/614154


Solution

  • You can also use the following script. Supporting Swift and Simulator as well.

    1. just add the content under Project Settings -> Build Phase as new Run Script Phase
    2. Build Project
    3. Folder will open, showing your zipped framework

    https://gist.github.com/PaulEhrhardt/6d2be145c5d1b51c216914e7f032013e

    #!/bin/sh
    
    # iOS universal library build script supporting swift modules inclusive simulator slices
    
    # prevention from running xcodebuild in a recusive way
    if [ "true" == ${ALREADYINVOKED:-false} ]; then
        echo "RECURSION: Detected, stopping"
    else
        export ALREADYINVOKED="true"
    
    # output directory for universal framework
    UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal
    mkdir -p "${UNIVERSAL_OUTPUTFOLDER}/iOS"
    
    # build both device and simulator versions for iOS
    xcodebuild -project "${PROJECT_NAME}.xcodeproj" -scheme "${PROJECT_NAME}"  -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 8' ONLY_ACTIVE_ARCH=NO clean build
    xcodebuild -project "${PROJECT_NAME}.xcodeproj" -scheme "${PROJECT_NAME}" -sdk iphoneos ONLY_ACTIVE_ARCH=NO clean build
    
    # copy the framework structure from iphoneos build to the universal folder
    cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework" "${UNIVERSAL_OUTPUTFOLDER}/iOS"
    
    # copy existing Swift modules from iphonesimulator build to the universal framework directory
    SIMULATOR_SWIFT_MODULES_DIR="${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule/"
    if [ -d "${SIMULATOR_SWIFT_MODULES_DIR}" ]; then
        cp -R "${SIMULATOR_SWIFT_MODULES_DIR}" "${UNIVERSAL_OUTPUTFOLDER}/iOS/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule"
    fi
    
    # create universal binary file using lipo and place the combined executable in the universal framework directory
    lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/iOS/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework/${PROJECT_NAME}"
    
    # intermediate step for copying the framework to the project's directory
    mkdir -p "${TMPDIR}/${PROJECT_NAME}/Frameworks/iOS"
    cp -R "${UNIVERSAL_OUTPUTFOLDER}/iOS/${PROJECT_NAME}.framework" "${TMPDIR}/${PROJECT_NAME}/Frameworks/iOS"
    
    # create a zip file and move it to the project's directory
    cd "${TMPDIR}/${PROJECT_NAME}/Frameworks/iOS"
    zip -r "${PROJECT_NAME}.framework.zip" "${PROJECT_NAME}.framework"
    mkdir -p "${PROJECT_DIR}/universal-framework"
    mv "${PROJECT_NAME}.framework.zip" "${PROJECT_DIR}/universal-framework"
    
    # optional: show the project's directory in Finder
    open "${PROJECT_DIR}/universal-framework"
    fi