How to export "fat" Cocoa Touch Framework (for Simulator and Device)? How to export "fat" Cocoa Touch Framework (for Simulator and Device)? ios ios

How to export "fat" Cocoa Touch Framework (for Simulator and Device)?


The actuality of this answer is: July 2015. It is most likely that things will change.

TLDR;

Currently Xcode does not have tools for automatic export of universal fat framework so developer must resort to manual usage of lipo tool. Also according to this radar before submission to AppStore developer who is framework's consumer also must use lipo to strip off simulator slices from a framework.

Longer answer follows


I did similar research in the topic (the link at the bottom of the answer).

I had not found any official documentation about distribution of so my research was based on exploration of Apple Developer Forums, Carthage and Realm projects and my own experiments with xcodebuild, lipo, codesign tools.

Here's long quote (with a bit of markup from me) from Apple Developer Forums thread Exporting app with embedded framework:

What is the proper way to export a framework from a framework project?

Currently the only way is exactly what you have done:

  • Build the target for both simulator and iOS device.
  • Navigate to Xcode's DerivedData folder for that project and lipo the two binaries together into one single framework. However, when you build the framework target in Xcode, make sure to adjust the target setting 'Build Active Architecture Only' to 'NO'. This will allow Xcode to build the target for multiple binarty types (arm64, armv7, etc). This would be why it works from Xcode but not as a standalone binary.

  • Also you'll want to make sure the scheme is set to a Release build and build the framework target against release. If you are still getting a library not loaded error, check the code slices in the framework.

  • Use lipo -info MyFramworkBinary and examine the result.

lipo -info MyFrameworkBinary

Result is i386 x86_64 armv7 arm64

  • Modern universal frameworks will include 4 slices, but could include more: i386 x86_64 armv7 arm64 If you don't see at least this 4 it coud be because of the Build Active Architecture setting.

This describes process pretty much the same as @skywinder did it in his answer.

This is how Carthage uses lipo and Realm uses lipo.


IMPORTANT DETAIL

There is radar: Xcode 6.1.1 & 6.2: iOS frameworks containing simulator slices can't be submitted to the App Store and a long discussion around it on Realm#1163 and Carthage#188 which ended in special workaround:

before submission to AppStore iOS framework binaries must be stripped off back from simulator slices

Carthage has special code: CopyFrameworks and corresponding piece of documentation:

This script works around an App Store submission bug triggered by universal binaries.

Realm has special script: strip-frameworks.sh and corresponding piece of documentation:

This step is required to work around an App Store submission bug when archiving universal binaries.

Also there is good article: Stripping Unwanted Architectures From Dynamic Libraries In Xcode.

I myself used Realm's strip-frameworks.sh which worked for me perfectly without any modifications though of course anyone is free to write a one from scratch.


The link to my topic which I recommend to read because it contains another aspect of this question: code signing - Creating iOS/OSX Frameworks: is it necessary to codesign them before distributing to other developers?


This is not so clear solution, but there is only way, that I find:

  1. Set ONLY_ACTIVE_ARCH=NO in the Build Settings

    • Build library for simulator
    • Build library for device
  2. Open in console Products folder for your framework (you can open it by open framework folder and cd .. from there)

enter image description hereenter image description here

  1. Run this script from Products folder. It creates fat Framework in this folder. (or do it manually as explained below in 3. 4.)

Or:

  1. Combine these 2 Frameworks using lipo by this script (replace YourFrameworkName to your Framework name)

    lipo -create -output "YourFrameworkName" "Debug-iphonesimulator/YourFrameworkName.framework/YourFrameworkName" "Debug-iphoneos/YourFrameworkName.framework/YourFrameworkName"
  2. Replace with new binary one of the existing frameworks:

    cp -R Debug-iphoneos/YourFrameworkName.framework ./YourFrameworkName.frameworkmv YourFrameworkName ./YourFrameworkName.framework/YourFrameworkName

  1. Profit: ./YourFrameworkName.framework - is ready-to-use fat binary! You can import it to your project!

For project, that not in Workspaces:

You can also try to use this gist as described here. But it seems, that it not works for projects in workspaces.


@Stainlav answer was very helpful but what I did instead was to compile two versions of the framework (one for the device and one for simulator) and then added the following Run Script Phase to automatically copy the precompiled framework required for the running architecture

echo "Copying frameworks for architecture: $CURRENT_ARCH"if [ "${CURRENT_ARCH}" = "x86_64" ] || [ "${CURRENT_ARCH}" = "i386" ]; then  cp -af "${SRCROOT}/Frameworks/Simulator/." "${SRCROOT}/Frameworks/Active"else  cp -af "${SRCROOT}/Frameworks/Device/." "${SRCROOT}/Frameworks/Active"fi

This way I don't have use lipo to create a fat framework neither the Realm's strip-frameworks.sh to remove the unnecessary slices when submitting to the App Store.