Ios9: Using Dynamic Framework with Simulator and Device

Swift Framework build fails for device but not for simulator

It sounds like you built the framework for a simulator and not for a device. When the linker is trying to link the application for a device, it doesn't find the framework built for that device.

Two of the ways to do it are as follows.

1) When building the framework, set the active scheme appropriate for the device (upper left area in Xcode). Then, before building the application for the device, go to Build Settings for the app and add the framework's location to Framework Search Paths. Make sure you pick the right binary! For example, when building for the iOS simulator, a debug binary of the framework is going to be in a directory called Build/Products/Debug-iphonesimulator.

With this approach you also need to add the framework to the Copy Files build phase of your app, specifying Destination as Frameworks.

2) Embed the framework into the application, make it a dependency of the app, and set up the application to link with the framework in the app's Build Phases. See

https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPFrameworks/Tasks/CreatingFrameworks.html

about embedding a framework and for other useful framework-related info. A convenient way of accomplishing this is to go to the General tab of your application target and add the framework in the Embedded Binaries section.

Alternatively, if you create your framework after creating the application, you can ask Xcode to embed the framework into the app.

iOS app with framework crashed on device, dyld: Library not loaded, Xcode 6 Beta

In the target's General tab, there is an Embedded Binaries field. When you add the framework there the crash is resolved.

Reference is here on Apple Developer Forums.

Copy text from iOS 9 simulator

This seems to be woking again on latest Xcode (7.3.1)

Creating a dylib for iOS simulator

I've managed to sort this one out myself (eventually) :-) The trick was to:

Add another target in Xcode, by using File|New|Target, and select Library from the OSX/Framework & Library items.

In the Build Settings for the new target:

In the Architectures section, since this dylib destined for just i386 simulator, in I added i386 to the Architectures item, and removed the others, and did the same for Valid Architectures

I set the Base SDK to iOS9.3

I changed the Supported Platforms to iOS

Since I didn't want the dylib put on the Mac, in the deployment section I set Skip Install to Yes

So that the install name doesn't have a specific path, in the linking section, I changed the Dynamic Library Install Name Base to @rpath

I also didn't want Xcode to add a prefix to the dylib, so in the Packaging section I removed the Executable Prefix value

I may have above and beyond with a couple of the settings, however the result is what I desired.

How to archive an app that includes a custom framework?

You actually don't need to put it in the "embedded binaries" section. You only need it in the "linked frameworks and libraries section. Make sure that your framework is a Universal Framework (meaning it can compile for all architectures), and make sure you have the right compiler flags set (-ObjC if your framework has any categories etc) There may be some other things you need to set as well like "Other C Flags" if your framework includes any c code and you want to enable bitcode in your client app then you should put "-fembed-bitcode" in your framework Other C Flags. Those were the things I needed to do to get my framework app to the store. I think its a just a misconception that you need to put this in embedded binaries as well to get it to archive for the store.

This is the build script I use to generate the universal framework. It builds right to my desktop. You can uncomment section 8 if your framework is in Swift. You want to create an aggregate target and add this as a run script in build phases.

# Merge Script

# 1
# Set bash script to exit immediately if any commands fail.
set -e

# 2
# Setup some constants for use later on.
FRAMEWORK_NAME="MyFramework"

# 3
# If remnants from a previous build exist, delete them.
if [ -d "${SRCROOT}/build" ]; then
rm -rf "${SRCROOT}/build"
fi

# 4
# Build the framework for device and for simulator (using
# all needed architectures).
xcodebuild -target "${FRAMEWORK_NAME}" -configuration Release -arch arm64 -arch armv7 -arch armv7s only_active_arch=no defines_module=yes -sdk "iphoneos"
xcodebuild -target "${FRAMEWORK_NAME}" -configuration Release -arch x86_64 -arch i386 only_active_arch=no defines_module=yes -sdk "iphonesimulator"

# 5
# Remove .framework file if exists on Desktop from previous run.
if [ -d "${HOME}/Desktop/${FRAMEWORK_NAME}.framework" ]; then
rm -rf "${HOME}/Desktop/${FRAMEWORK_NAME}.framework"
fi

# 6
# Copy the device version of framework to Desktop.
cp -r "${SRCROOT}/build/Release-iphoneos/${FRAMEWORK_NAME}.framework" "${HOME}/Desktop/${FRAMEWORK_NAME}.framework"

# 7
# Replace the framework executable within the framework with
# a new version created by merging the device and simulator
# frameworks' executables with lipo.
lipo -create -output "${HOME}/Desktop/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}" "${SRCROOT}/build/Release-iphoneos/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}" "${SRCROOT}/build/Release-iphonesimulator/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}"

# 8
# Copy the Swift module mappings for the simulator into the
# framework. The device mappings already exist from step 6.
#cp -r "${SRCROOT}/build/Release-iphonesimulator/${FRAMEWORK_NAME}.framework/Modules/${FRAMEWORK_NAME}.swiftmodule/" "${HOME}/Desktop/${FRAMEWORK_NAME}.framework/Modules/${FRAMEWORK_NAME}.swiftmodule"

# 9
# Delete the most recent build.
if [ -d "${SRCROOT}/build" ]; then
rm -rf "${SRCROOT}/build"
fi

Once your framework is on the desktop, if you go inside of it there will be a text document with the same name as your framework. If you navigate to that and run the command "lipo -info" on it in terminal you should get the following output:

Architectures in the fat file: MyFramework are: armv7 armv7s i386 x86_64 arm64 


Related Topics



Leave a reply



Submit