Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly set up a Podfile that has both a framework target and an application target that uses that framework

Having the following project structure:

  • Application target resides on an Xcode project (App.xcodeproj) depends on a Framework target that resides on a different Xcode project (Framework.xcodeproj).
  • Application target depends on Material pod
  • Framework target depends on AFNetworking pod.

Is this kind of setup supported by CocoaPods?

My first attempt was to do something like this:

platform :ios, '9.0'
use_frameworks!

target 'App' do
  workspace 'App.xcworkspace'
  project 'App.xcodeproj'  
  pod 'Material'
end

target 'Framework' do
  workspace 'App.xcworkspace'
  project 'Framework/Framework.xcodeproj'
  pod 'AFNetworking'
end

But the application crashes on launch with the following error:

dyld: Library not loaded: @rpath/AFNetworking.framework/AFNetworking

Referenced from: /Users/ruenzuo/Library/Developer/Xcode/DerivedData/App-aayvulxvruuarudtheuilepmmctk/Build/Products/Debug-iphonesimulator/Framework.framework/Framework

Reason: image not found

Which makes sense, because CocoaPods doesn't know that the App and Framework are related. In fact, after pod install I get the following warning:

[!] The Podfile contains framework targets, for which the Podfile does not contain host targets (targets which embed the framework).
If this project is for doing framework development, you can ignore this message. Otherwise, add a target to the Podfile that embeds these frameworks to make this message go away (e.g. a test target).

I then tried embedding the Framework target into the Application target, like this:

platform :ios, '9.0'

use_frameworks!

target 'App' do
  workspace 'App.xcworkspace'
  project 'App.xcodeproj'
  pod 'Material'
  target 'Framework' do
    workspace 'App.xcworkspace'
    project 'Framework/Framework.xcodeproj'
    pod 'AFNetworking'
  end
end

But it didn't work. The only way I managed to get it working was like:

platform :ios, '9.0'
use_frameworks!

target 'App' do
  workspace 'App.xcworkspace'
  project 'App.xcodeproj'
  pod 'Material'
  pod 'AFNetworking'
end

target 'Framework' do
  workspace 'App.xcworkspace'
  project 'Framework/Framework.xcodeproj'
  pod 'AFNetworking'
end

But having the AFNetworking pod repeated feels like I'm doing something wrong. Also, I didn't manage to get rid of the warning, so obviously I'm just forcing this to work by having CocoaPods copying over the AFNetworking pod to the application Frameworks directory.

Has anyone managed to get something like this working?

like image 555
UnsafePointer Avatar asked Apr 13 '17 19:04

UnsafePointer


People also ask

What is use frameworks in Podfile?

use_frameworks! in the Podfile means that we want the listed frameworks to be dynamically installed instead as static frameworks. Thank you but please give more details about dynamically install vs static install.

How do I use CocoaPods with my internal iOS frameworks?

If you've used CocoaPods before, you probably know that the usual way to get started using it is by running pod init in the directory that contains the . xcproject file. Running this command creates a workspace that CocoaPods can use to manage the dependencies.


1 Answers

The trick is to add your framework target to the "Target Dependencies" build phase of your application target, like this:

Xcode screenshot

For some reason CocoaPods relies on "Target Dependencies" – it's not enough to have your library/framework in the "Link With Binary Libraries" phase. Your Podfile should look like this:

project 'CocoapodsTest'

target 'CocoapodsTest' do
    pod 'AFNetworking'
end

target 'SampleLibrary' do
    pod 'Masonry'
end

After running pod install, you can inspect the "Other Linker Flags" of your application target to verify that it forwards both your app's and your libraries' dependencies to the linker:

Xcode screenshot

like image 169
Tamás Zahola Avatar answered Sep 30 '22 13:09

Tamás Zahola