Search code examples
iosxcodeios-frameworks

How to embed framework which depends on architecture


My iOS project depends on some CustomLib.framework. This framework comes as two separate files: one for simulator, another for real device.

I put these files into folders

@(PROJECT_DIR)/device_frameworks/CustomLib.framework

and

@(PROJECT_DIR)/simulator_frameworks/CustomLib.framework

I edited

Build Settings->Search paths->Framework search paths:

Any iOS Simulator SDK     $(PROJECT_DIR)/simulator_frameworks
Any iOS SDK               $(PROJECT_DIR)/device_frameworks

What I do not understand is how to add CustomLib.framework to the Xcode project, and how to add it as embedded framework (Build phases->Embed frameworks). Because on these steps I have to specify a concrete framework, but I have two separate frameworks in device_frameworks and simulator_frameworks folders.


Solution

  • There is no way in xcode to include them depending on architecture, but Apple introduced 2019 XCFrameworks as a new code distribution format.

    From XCode 11 release notes:

    An XCFramework makes it possible to bundle a binary framework or library for multiple platforms —including iOS devices, iOS simulators, and Mac Catalyst — into a single distributable .xcframework bundle that your developers can use within their own applications.

    You can use this to bundle both of those frameworks into one, add that to your projects and Xcode uses the right platform’s version of the included framework or library at build time.

    You can create an XCFramework from Terminal by using the following command:

    xcodebuild -create-xcframework -framework PATH_TO_FRAMEWORK1 -framework PATH_TO_FRAMEWORK2 -output PATH_TO_BUNDLED_FRAMEWORK
    

    Where PATH_TO_FRAMEWORK1 and PATH_TO_FRAMEWORK2 are the paths to your frameworks and PATH_TO_BUNDLED_FRAMEWORK is the path of the resulting XCFramework.

    Assuming you're in your project dir, the command could look like this:

    xcodebuild -create-xcframework -framework device_frameworks/CustomLib.framework -framework simulator_frameworks/CustomLib.framework -output BundledCustomLib.xcframework
    

    Then you would only need to add BundledCustomLib.xcframework to your project.