XCFramework is a new framework encapsulation format provided by Apple. It will be released starting in 2019. XCFramework should be used as a unified binary framework format to replace the existing static and dynamic library formats. In this series of articles we will explore the creation and use of various frameworks.

This article first looks at the production and use of Swift static library. The existing Swift static libraries end up with.a files and header files. The XCFramework static libraries are further encapsulated on the basis of.a files, but the essence remains the same. Here’s a look at creating the XCFramework Swift static library from scratch.

Create a static library project

  • Create as before, select Static Library:

  • Create an include folder in your project and place the headers that Xcode automatically generates when it is compiled in objective-C:

  • Edit the build script, take the header file from the DerivedData directory, and copy the header file to the include folderSwiftStaticLibraryExample-Swift.hAdd Xcode to include directory, which will be automatically updated by subsequent compilation:
# Type a script or drag a script file from your workspace to insert its path.
cp ${DERIVED_FILES_DIR}/${TARGET_NAME}-Swift.h ${SRCROOT}/include/
Copy the code
  • Write a simple class to test:
// SwiftStaticLibraryExample.swift
import Foundation

@objc public class SwiftStaticLibraryExample: NSObject {
  @objc public var title = "Swift Static library is working."
}
Copy the code

Generate XCFramework files

  • After compiling, you can see the generated Products directory.aFile, open this file in Finder and copy it to the XCFramework production directory, where there’s another one.swiftmoduleFile, which is used for Swift code calls, but not for now:

  • The XCFramework for the current example only needs to be put in.aRun the files for emulators and iOS devices respectively at compile time.aFiles are placed in folders at the same time, so that the XCFramework can run on both the emulator and the real machine:

  • Enter on the terminalSwift Static XCFrameworkFolder, execute the command, will be two.aFiles are listed:
xcodebuild -create-xcframework -library device/libSwiftStaticLibraryExample.a -library simulator/libSwiftStaticLibraryExample.a -output ./SwiftStaticLibraryExample.xcframework
Copy the code
  • Xcode automatically selects the corresponding file for execution when it is running:

Use XCFramework static libraries in objective-C projects

  • Create a default Objective-C iOS project, add a lib directory to the project directory, and drag the XCFramework file you generated into Xcode:

  • Add static library link to project:

  • Create an empty Swift file in the project, or compile an error:

  • Ensure that the Swift version in Build Settings is compatible:

  • Set Header Search Paths to in the TARGETS Build Settings${SRCROOT}/libAnd select Recursive.
  • Add a dynamic library header file to the ObjC project and call the test class.
#import "ViewController.h"
#import "SwiftStaticLibraryExample-Swift.h"

@interface ViewController(a)

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  NSString *title = [SwiftStaticLibraryExample new].title;
  NSLog(@ "% @", title);
}

@end

// Swift Static library is working.
Copy the code

Use the XCFramework static library in the Swift project

  • Create a default Swift iOS project, add a lib directory to the project directory, drag and drop the XCFramework file generated earlier into Xcode, and compile the static library.swiftmoduleThe files are also placed in the lib directory, notice.swiftmoduleThe files are also generated in both the real and simulator versions, merging the two files into one.swiftmoduleFile, and note the selection when adding to the projectCreate folder referencesTo avoid possible file name conflicts during subsequent project compilation:

  • Add static library links to your project (see the previous section)
  • Set Library Search Paths in Build Settings:${SRCROOT}/lib, be careful to chooserecursiveThis is used to find XCFramework files.
  • Set Import Paths in Build Settings:${SRCROOT}/lib, be careful to chooserecursiveThis is for searching.swiftmoduleFile used to determine the interface to the Swift static library.
  • Reference the static library in your code after setting it up:
import UIKit
import SwiftStaticLibraryExample

class ViewController: UIViewController {

  override func viewDidLoad(a) {
    super.viewDidLoad()
    let title = SwiftStaticLibraryExample().title
    print(title)
  }
}
Copy the code
  • Note that bitcode may need to be turned off when archiving in the App project, as the Swift static library cannot generate bitcode.
  • Remove in packaged resources.swiftmoduleFiles in the x86_64 schema, which are not needed for Archive generation, can be deleted by searching:

conclusion

The XCFramework is essentially the same as the static and dynamic libraries of the past. The encapsulation form is the same as before, and the usage method is basically the same as before. The Settings required before are the same now (such as header files, import methods, swiftModule, etc.). But Xcode will gradually integrate the XCFramework into something easier to use, including integration with Swift Package Manager, which is better for Package management and the third-party library ecosystem.

Github-xcframeworkexample github-xcframeworkExample