Skip to content

Instantly share code, notes, and snippets.

@lalkrishna
Created December 16, 2020 19:42
Show Gist options
  • Save lalkrishna/c1b33cba4dbacddbc1ecc9d619877c42 to your computer and use it in GitHub Desktop.
Save lalkrishna/c1b33cba4dbacddbc1ecc9d619877c42 to your computer and use it in GitHub Desktop.
Create xcframework instead of Universal(fat) frameworks.

Advantages of xcframework

  • Support for multiple platforms in the same framework.
  • Support for Swift, Objective-C and C.
  • Support for dynamic frameworks and static libraries.
  • Support for Swift Package Manager.
  • End of fat binaries.

Steps to create Aggregate target:

  1. Open Current framework project
  2. File -> New -> Target
  3. Choose 'Other' tab -> Aggregate
  4. I just named that target as 'MyFramworkUniversal'
  5. Goto Build Phases -> Tap + Button -> New Run Script Phase. Then Add the code below.
#Gerenare device framework
xcodebuild archive -scheme ${PROJECT_NAME} -archivePath "${PROJECT_DIR}/build/${PROJECT_NAME}-iphoneos.xcarchive" -sdk iphoneos SKIP_INSTALL=NO BUILD_LIBRARIES_FOR_DISTRIBUTION=YES

#Generate simulator framework
xcodebuild archive -scheme ${PROJECT_NAME} -archivePath "${PROJECT_DIR}/build/${PROJECT_NAME}-iossimulator.xcarchive" -sdk iphonesimulator SKIP_INSTALL=NO BUILD_LIBRARIES_FOR_DISTRIBUTION=YES

#Generate xcframework for both arches
xcodebuild -create-xcframework -framework "${PROJECT_DIR}/build/${PROJECT_NAME}-iphoneos.xcarchive/Products/Library/Frameworks/${PROJECT_NAME}.framework" -framework "${PROJECT_DIR}/build/${PROJECT_NAME}-iossimulator.xcarchive/Products/Library/Frameworks/${PROJECT_NAME}.framework" -output "${PROJECT_DIR}/build/${PROJECT_NAME}.xcframework"

#Open directory where xcframework were generate
open "${PROJECT_DIR}/build"

Generate xcframework

Prerequisites

  • Set Build Libraries for Distribution to Yes in Build settings.
  • Set Skip Install to No in Build settings. Above steps are just to make confirm that the above flags are set.

Steps

  • Select 'MyFramworkUniversal' scheme & run target as 'Any iOS Device'
  • Build and Run
  • Script will automatically open the directory where the xcframework were generated.

References

https://www.appcoda.com/xcframework/ https://andresalla.dev/en/2020/06/02/the-end-of-fat-binaries-xcframeworks/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment