Automatically adding a dynamic library to embedded frameworks via Cocoapods

5k Views Asked by At

I'm creating a custom Objective-C framework (dynamic library) and attempting to pull it into a consumer application using Cocoapods. If I don't add the framework as an embedded binary, I get a nasty runtime error:

dyld: Library not loaded: @rpath/MyFramework.framework/MyFramework
Referenced from: /private/var/mobile/Containers/Bundle/Application/XXX/MyFramework.app/MyFramework
Reason: image not found

My understanding is that as a 3rd party custom framework, I have to add it as an embedded binary (due to Apple's concerns about dynamically linking random frameworks). Is that right?

If not, can I change my framework or Podspec file somehow so I don't need to embed it (without making it a static library)?

Otherwise, is there a way to automate adding the framework to embedded binaries on pod install?

2

There are 2 best solutions below

2
On

By default, cocoapods tries to link pods as static libraries. You can force cocoapods to use frameworks instead by adding use_frameworks! in your Podfile. Or you can probably try to set appropriately xcconfig or/and vendored_framework in your library podspec file to link with your framework. I've seen a good example in Sparkle project's podspec file. Sparkle uses a vendored_framework workaround to get dynamic linking working in OSX (i believe because of this issue in cocoapods repo).

2
On

I have had this exact issue (Objective C framework built as Module with dynamic library) and finally found a solution:

After pod install, you must go to the main project's Build Phases in Embed Pods Frameworks and add MyFramework.framework to the Input Files section. If your Framework is distributed with a bundle, you must add it to the Copy Pods Resources section in the same tab.

Unfortunately, this must be done by the person using your framework. If you have a solution that does not require anything on the user's side, please let me know.

See Screenshot here

Update:

It seems like if you do not specify a module_name and add you add your framework to vendored_frameworks inside the podspec, the steps I mentioned above are not needed, so no interaction is needed on user's side.