XCFramework

スポンサーリンク

iOS Framework

When we want to create library for iOS, we have several choices

  • Static Library (Objective-C)
  • Static Framework (Objective-C)
  • Universal Framework (Swift)

In Swift, we should Dynamic Framework, but have one big problem.

Universal framework includes both simulator and real device library inside, but when we submit App into AppStore, we should remove simulator library from Dynamic framework, always.

I’m mobile SDK developer, and we always tell our customers to run script to remove library for simulator. This is annoying for app developers.

Binary Frameworks = xcframework

XCFramework resolve this issue. Of course, XCFramework contains both simulator and realdevice library inside.

We don’t need to prepare to remove script compare to traditional Universal Framework for Swift. It’s good point.

Let’s start

O.K. Let’s start to create swift library.

Choose Framework from list

Choose Swift. Project name is “FirstSexyFramework”

I changed the setting Deployment target as iOS 10.0. Developers can use from iOS10.0

To test this library, I added Sandbox App

And add this library to dependencies

Library codes

FirstClass.swift

This is library codes. To support use from Objective-C Class extends NSObject and add @objc to function

import Foundation

public class FirstClass : NSObject {
    
    @objc public func helloWorld() {
        print("HelloWorld!")
    }
}

Use from Sandbox

Before building framework, test from sandbox app.

Viewcontroller.swift

import UIKit
import FirstSexyFramework

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let first = FirstClass()
        first.helloWorld()
    }
}

Import Framework and call above class and method

Build

We need special build script to make XCFramework now.

Add Build target (Aggregate)

And add Run Script

Script

# Set Properties Output Directory
OUTPUT_DIR=${PROJECT_DIR}/Output
# Intermediate files
DERIVED_DIR=${OUTPUT_DIR}/${CONFIGURATION}-derived
# Archives
ARCHIVE_DIR=${OUTPUT_DIR}/${CONFIGURATION}-archive
# XCFramework
XCFRAMEWORK_DIR=${OUTPUT_DIR}/${CONFIGURATION}-xcframework

# Delete Output Directory first
rm -rf ${OUTPUT_DIR}
# Create Intermediate Directory
mkdir -p ${DERIVED_DIR}
# Create Archive Directory
mkdir -p ${ARCHIVE_DIR}
# Create xcframework Directory
mkdir -p ${XCFRAMEWORK_DIR}

# iOS Device Archive File Path
ARCHIVE_FILE_IOS=${ARCHIVE_DIR}/ios.xcarchive
echo "ARCHIVE_FILE_IOS:${ARCHIVE_FILE_IOS}"
# iOS Simulator Archive File Path
ARCHIVE_FILE_IOS_SIMULATOR=${ARCHIVE_DIR}/iossimulator.xcarchive
echo "ARCHIVE_FILE_IOS_SIMULATOR:${ARCHIVE_FILE_IOS_SIMULATOR}"

# Build iOS device Archive
xcodebuild archive -scheme ${PROJECT_NAME} -destination="iOS" -archivePath $ARCHIVE_FILE_IOS -derivedDataPath $DERIVED_DIR -sdk iphoneos SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES

# Build iOS simulator Archive
xcodebuild archive -scheme ${PROJECT_NAME} -destination="iOS Simulator" -archivePath $ARCHIVE_FILE_IOS_SIMULATOR -derivedDataPath $DERIVED_DIR -sdk iphonesimulator SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES

# Create xcframework
xcodebuild -create-xcframework -framework $ARCHIVE_FILE_IOS/Products/Library/Frameworks/${PROJECT_NAME}.framework -framework $ARCHIVE_FILE_IOS_SIMULATOR/Products/Library/Frameworks/${PROJECT_NAME}.framework -output $XCFRAMEWORK_DIR/${PROJECT_NAME}.xcframework

# Open Finder
open ${XCFRAMEWORK_DIR}

We can see XCFramework under finder.

This contains arm7, i386 etc…

iOS
スポンサーリンク
Professional Programmer2

コメント