dyld: Library not loaded with XCFramework for device but works with Simulator

Viewed 1622

I built a universal XCFramework by following this guide https://medium.com/@anuragajwani/how-to-build-universal-ios-static-libraries-using-xcframework-a3f70f998c38, using the following commands:

xcodebuild build \
  -scheme myApp \
  -derivedDataPath derived_data \
  -arch arm64 \
  -sdk iphoneos \
  BUILD_LIBRARY_FOR_DISTRIBUTION=YES
mkdir -p build/devices
cp -r derived_data/Build/Products/Debug-iphoneos/ build/devices

xcodebuild build \
  -scheme myApp \
  -derivedDataPath derived_data \
  -arch x86_64 \
  -sdk iphonesimulator \
  BUILD_LIBRARY_FOR_DISTRIBUTION=YES
mkdir -p build/simulators
cp -r derived_data/Build/Products/Debug-iphonesimulator/ build/simulators

xcodebuild -create-xcframework \
    -framework build/simulators/myApp.framework \
    -framework build/devices/myApp.framework \
    -output build/myApp.xcframework

This built a myApp.xcframework, which contains both ios-arm64 and ios-x86_64-simulator directories.

I'd like to distribute this to a few customers and both devices and Simulators should be supported.

I imported myApp.xcframework into a different project, and in General made sure that the framework is listed and "Embed & Sign" is set.

I can run the project and make use of the framework just fine with the simulator. However, when I try to run it on the device I get the following:

dyld: Library not loaded: @rpath/myApp.framework/myApp
  Referenced from: /private/var/containers/Bundle/Application/16EC2405-9C65-4DED-BFF6-4A2546397BC9/myApp.app/myApp
  Reason: image not found

If I, however, only use the myApp.framework from the ios-arm64 directory of the XCFramework and import this, the application works on the device. It seems the problem is not with the built framework itself, but with it not being able to locate the correct framework for a device.

Any help would be greatly appreciated!

1 Answers

"Embed & Sign" means that you are going to use a dynamic library and the compiler just copy a platform depended entity to YourApp.app/Frameworks/ so .a file causes your error in this folder since a static library can't be loaded as a dynamic one.

To fix your issue you should use "Do Not Embed" and in this case your static entity will be linked into your app and you can check this in Link Binary With Libraries:

enter image description here

Related