• This article was translated from github.com/bielikb/xcf… Thanks to the author

This project demonstrates creating and integrating xcFrameworks and working with static libraries and Swift Packages in the same Xcode project.

directory

  • This section describes the new format of the XCFramework
  • How do I create the XCFramework for IOS and IOS emulators
  • Use create_xcframeworks. Sh to generate xcframeworks for ios and ios emulators
  • Testing and troubleshooting
  • Xcframeworks distribution
  • How do I integrate the XCFramework into a project
  • The working directory structure of the XCFrameworks
  • Other information

Lead requirements

  • Xcode 11
  • Swift 5.1 Toolchain – Run in terminalsudo xcode-select -s path/to/Xcode11
  • Github/Gitlab/Bitbucket is pre-set in Xcode’s account Settings

This section describes the new format of the XCFramework

requirements

  • Xcode11
  • Swift 5.1 and above

Motivation & Results

  • Standard formats are introduced to achieve modular stability of the Swift framework and libraries. Library authors and users no longer need to use the same version of the compiler.
  • Provide a seamless experience when creating and integrating mode-stabilization frameworks (note: just drag it in like old frameworks, but most people still use Cocospods and the XCFramework will be added to Cocospods 1.9)
  • Support for all Apple platforms and architectures

Note: Although “FAT Framework” can support module stability, Apple does not officially support the “Lipo” command line tool for fusing frameworks together. In the case of fusing two platforms with similar architectures, using lipo tools is also inadequate. The ARM6 architecture can be found in iOS + watchOS.

  • STOPCreate and usefat frameworks== This parameter is not requiredlipo.
  • STOPThrough customization in the projectbuild-phase(Construction phase) Stripping the architecture to separate frameworks

The content of the xcframwork

This format packages Module stable Frameworks (.swiftInterface).

[Info. Plist] (. / Products/xcframeworks/DynamicFramework. Xcframework) bundle contains all frameworks available framework. Xcode uses this information during linking => XCodeBuild selects the correct framework framework for the platform we are building

The structure of the XCFramework looks like this

The size of the xcframework

The size of “XCFramework” is smaller than that of the corresponding “FAT Framework”. I tested the pure SWIFT and OC hybrid framework. Typically, the “LIPo” command line tool adds some overhead (larger) to all the included architectures

Platforms

Xcframework supports all Apple platforms – iOS, macOS, tvOS, watchOS, iPadOS, carPlayOS.

List of destinations

Platform Destination
iOS generic/platform=iOS
iOS Simulator generic/platform=iOS Simulator
iPadOS generic/platform=iPadOS
iPadOS Simulator generic/platform=iPadOS Simulator
macOS generic/platform=macOS
tvOS generic/platform=tvOS
watchOS generic/platform=watchOS
watchOS Simulator generic/platform=watchOS Simulator
carPlayOS generic/platform=carPlayOS
carPlayOS Simulator generic/platform=carPlayOS Simulator

How do I create the XCFramework for IOS and IOS emulators

1. Document required platforms

1.1 (command line) Pass SKIP_INSTALL=NO && BUILD_LIBRARY_FOR_DISTRIBUTION=YES for packaging

xcodebuild archive \
-workspace MyWorkspace.xcworkspace \
-scheme MyScheme \
-destination destination="generic/platform=iOS" \
-archivePath "archives/MyScheme-iOS" \
SKIP_INSTALL=NO \
BUILD_LIBRARY_FOR_DISTRIBUTION=YES
Copy the code

1.2 iOS Simulator – Specify the corresponding destion parameter destination=”generic/platform=iOS Simulator” and specify the corresponding path. For example, archives/MyScheme – iOS – Simulator.

xcodebuild archive \
..
-destination destination="generic/platform=iOS Simulator" \
-archivePath "archives/MyScheme-iOS-Simulator"\..Copy the code

1.3 iOS – (iOS real) Specify the iOS platform destination=”generic/platform=iOS” and specify the path for packaging the iOS real. The schema path is the same as 1.2, changed to myscheme-ios

xcodebuild archive \
..
-destination destination="generic/platform=iOS" \
-archivePath "archives/MyScheme-iOS"\..Copy the code

directory

Binary product files in.xcarchive in:

  • Products/Library/FrameworksDynamic library Framework directory
  • Products/usr/local/libStatic library directory

2. Create the XCFramework from the artifact

Xcodebuild allows you to create xcFramework by specifying frameworks, libraries, and even adding headers to libraries.

1. Specify all frameworks or libraries to be added to the. Xcframework
2. Specify the output path and run -output. Don’t forget to add.xcFramework to your output path
xcodebuild -create-xcframework \
           -framework My-iOS.framework \
           -framework My-iOS_Simulator.framework \
           -output My.xcframework
Copy the code

Module stability comes from Xcode11 + Swift 5.1. Once your module declares a.swiftInterface file (which describes the framework’s common interface as well as the linker flag, toolchain used, and other information). The Swift Interface file, which you can find in the SwiftModule folder of the framework is automatically generated when the XCFramework is created.

Use create_xcframeworks. Sh to generate xcframeworks for ios and ios emulators

The archiving and creation of.xcFramework is performed by the [create_xcframeworks. Sh] (/ scripts/create_xcframeworks.

This script takes one argument (the output directory). Output directory Will create subfolders for archives and xcFrameworks.

This script will:

  • According to the schemeStaticLibraryArchive and create.xcFramework (generate.xcFramework from static libraries)
  • According to the schemeDynamicFrameworkArchive and create.xcFramework (generate.XCFramework from dynamic libraries)

Method of use

/scripts/create_xcframeworks. Sh Output directoryCopy the code

For example.

./scripts/create_xcframeworks.sh Products
Copy the code

Testing and troubleshooting

Be sure to build and run the generated XCFramework before distributing it to customers. Very few problems will be raised at compile or run time, so don’t just rely on the success of the XCFramework creation (don’t assume that the xcFramWork creation is normal)

Below is a list of compiler errors I encountered while building the XCFramework and integrating it into the Xcode project.

The problem severity describe The solution
Redundant conformance of x to NSObjectProtocol error – thrown at dynamic linking time Your class is already subclassed from NSObject, which conforms to NSObjectProtocol Check protocol conformances of your classes and remove redundant conformance to NSObjectProtocol
Use of unimplemented initializer ‘init()’ for class error – thrown at dynamic linking time Objective-C ABI public classes need to provide public init Provide public init override for your public class: override public init()
@objc’ class method in extension of subclass of Class XThe requires iOS 13.0.0 error Rules for interoperability with Objective-C has changed since iOS 13.0.0. and currently doesn’t support @objc interoperability in class extensions. There’s open question on Swift forums Move/Remove @objc declaration from your Swift class extension
scoped imports are not yet supported in module interfaces warning Read more about Swift import declarations here: nshipster.com/import/ Import the module instead of specific declaration. For example: change import class MyModule.MyClass to import MyModule

Xcframeworks distribution

  • Manual Integration – available from today (available now)

  • Carthage

    • Xcframework is released as binary — available today
    • Roadmap to provide support for xcframeworks 2019/2020
  • CocoaPods

    • Feature Request to bring support for new XCFramework Format in V1.9.0 + (pr)[github.com/CocoaPods/C…]
  • Swift Package Manager

    • SE-0272 Package Manager Binary Dependencies (returned for revision)

How do I integrate the XCFramework into a project

  1. Manually drag and drop the XCFramework into the project target

  2. Embed &sign.xcFramework in the project target into your project targert


The working directory structure of the XCFrameworks

The XCFrameworks workspace includes:

  • StaticLibraryProject – Static library project
  • DynamicFrameworkProject – Dynamic library dylib project
  • Swift Package– Swift Package project (including example project)
  • TextAttributes– Externally referenced Swift Package
  • Sample– Example project Bohol has all the dependencies above


Other information

Binary Frameworks in Swift

Developer.apple.com/videos/play…

ABI Stability & Module Stability – swift.org

Swift.org/blog/abi-st…

Library evolution for stable ABIs

Github.com/apple/swift…

Library evolution – Docs

Github.com/apple/swift…

Swift Unwrapped – Swift 5.1 with Doug Gregor (Library Evolution…)

The spec. FM/podcasts/sw…

Alexis Beingessner- How Swift Achieved Dynamic Linking Where Rust Couldn’t

Gankra. Making. IO/blah/swift -…

Presentation about Dependency management in Xcode 11

www.slideshare.net/BorisBielik…