Using CocoaPods with multiple projects
Asked Answered
D

5

30

I have a workspace that contains:

  • myiPhone.xcodeproj
  • sharedStuff/sharedStuff.xcodeproj

sharedStuff.xcodeproj builds a static library that is a dependency to myiPhone.xcodeproj (for simplicity assume that each project has a single target).

Now I want to add a library through CocoaPods that should be available to both projects.

My Podsfile looks like this:

workspace 'myWorkspace.xcworkspace'
platform :ios

target :myiPhone do
    xcodeproj 'myiPhone.xcodeproj'
    pod 'MBProgressHUD', '~> 0.6'
end


target :sharedStuff do
    xcodeproj 'sharedStuff/sharedStuff.xcodeproj'
    pod 'MBProgressHUD', '~> 0.6'
end

When I build I get these errors:

diff: /../Podfile.lock: No such file or directory diff: /Manifest.lock: No such file or directory error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.

Anyone have a clue what's going on here?

UPDATE: From the looks of it the PODS_ROOT variable is not set when the "Check Pods Manifest.lock" build phase is executed.

Drowsy answered 28/5, 2013 at 14:58 Comment(1)
I get the same sort of problem. A solution to this would be nice!Nickerson
A
10

The first targets in your xcode projects have a build phase to perform a diff on two lock files. But it seems like your xcode projects configurations are not referencing the user defined settings configured in Pods/Pods-libPods.xcconfig.

It looks like you are trying to link a Pod with specific targets in multiple xcodeprojs. If my assumption is correct, you are using the target attribute incorrectly. The target attribute creates a new static library within the Pods project that includes the Pods you configured within that target.

The default target for the Pods xcodeproj is libPods which generates the libPods.a static library. This is generated if you do not specify a target. So if you don't care about generating multiple static libaries in the Pods xcodeproj, don't bother defining a target and use the link_with attribute to link the default libPods target (static library) to the targets in your xcodeprojs.

For example, the following Podfile will create a libPods target in Pods.xcodeproj which will add MBProgressHUD sources to the compile phase then add the xcconfig file defining PODS_ROOT and the PODS_HEADER_SEARCH_PATH for example to each of your xcodeprojs. It will then link this static library to the targets you specified with link_with and xcodeproj

workspace 'myWorkspace.xcworkspace'
platform :ios

xcodeproj 'myiPhone.xcodeproj'
link_with 'myiPhone'
xcodeproj 'sharedStuff/sharedStuff.xcodeproj'
link_with 'sharedStuff'

pod 'MBProgressHUD', '~> 0.6'
Astigmia answered 5/6, 2013 at 19:53 Comment(4)
You are right about target vs link_with. It seems the build phase that checks the Podfile.lock is not updated correctly so I had to delete it manually and do pod install. Also I realised it's incorrect to link a pod with my main target and with a target that produces a static library that gets linked with my main target since it causes duplicate symbols. This makes sense though and is not a CocoaPods problem.Drowsy
@MihaiDamian What did you end up doing regarding the duplicate symbols. Ultimately you want to pods library to be linked only to the main app target, and not the static lib as well.Sianna
This doesn't work for me. Only the last link_with statement is getting honored. So in the above example, the only target linked with Pods.a would be "sharedStuff".Bluma
@AndreyGordeev I ended up using the cocoapods-packager tool to build a framework from each pod, then I drag those into XCode. I don't use the Cocoapods integration with XCode. If your dependencies support it you could also use the newer package manager Carthage which basically takes this approach.Bluma
P
27

I have 2 projects in my Workspace and the accepted answer didn't work for me. But finally I've managed how to get Cocoapods working properly with 2 projects. Here is how my pod file looks like:

workspace 'Projects.xcworkspace'
platform :ios, '8.0'

use_frameworks!

# ignore all warnings from all pods
inhibit_all_warnings!

def shared_pods
    # all the pods go here
    # pod 'Parse' etc.
end

xcodeproj 'Project1.xcodeproj'
xcodeproj 'Project2/Project2.xcodeproj'

target :Project1 do
  xcodeproj 'Project1'
  shared_pods
end

target :Project2 do
  xcodeproj 'Project2/Project2.xcodeproj'
  shared_pods
end
Preempt answered 19/11, 2015 at 15:25 Comment(5)
If your project 2 is a dependency for project 1, wouldn't this result in duplicate symbols?Bertrando
No. Why this would cause duplicating symbols?Preempt
@RahulVyas I don't use this method anymore. Have you resolved the issue? If yes, feel free to edit an answer.Preempt
@AndreyGordeev yes the issue was actually my own mistake I was building for device and I didn't included any provision profile.Raquel
Also cocoapods now has different mechanism for shared pods. Let me post that as an answerRaquel
A
20

With current syntax it looks like this

use_frameworks!
workspace 'myWorkspace'

project 'myiPhone'
project 'sharedStuff/sharedStuff'


target 'myiPhone' do
    project 'myiPhone'
    pod 'MBProgressHUD', '~> 0.6'
end


target 'sharedStuff' do
    project 'sharedStuff/sharedStuff'
    pod 'MBProgressHUD', '~> 0.6'
end
Atworth answered 3/10, 2018 at 3:15 Comment(0)
A
10

The first targets in your xcode projects have a build phase to perform a diff on two lock files. But it seems like your xcode projects configurations are not referencing the user defined settings configured in Pods/Pods-libPods.xcconfig.

It looks like you are trying to link a Pod with specific targets in multiple xcodeprojs. If my assumption is correct, you are using the target attribute incorrectly. The target attribute creates a new static library within the Pods project that includes the Pods you configured within that target.

The default target for the Pods xcodeproj is libPods which generates the libPods.a static library. This is generated if you do not specify a target. So if you don't care about generating multiple static libaries in the Pods xcodeproj, don't bother defining a target and use the link_with attribute to link the default libPods target (static library) to the targets in your xcodeprojs.

For example, the following Podfile will create a libPods target in Pods.xcodeproj which will add MBProgressHUD sources to the compile phase then add the xcconfig file defining PODS_ROOT and the PODS_HEADER_SEARCH_PATH for example to each of your xcodeprojs. It will then link this static library to the targets you specified with link_with and xcodeproj

workspace 'myWorkspace.xcworkspace'
platform :ios

xcodeproj 'myiPhone.xcodeproj'
link_with 'myiPhone'
xcodeproj 'sharedStuff/sharedStuff.xcodeproj'
link_with 'sharedStuff'

pod 'MBProgressHUD', '~> 0.6'
Astigmia answered 5/6, 2013 at 19:53 Comment(4)
You are right about target vs link_with. It seems the build phase that checks the Podfile.lock is not updated correctly so I had to delete it manually and do pod install. Also I realised it's incorrect to link a pod with my main target and with a target that produces a static library that gets linked with my main target since it causes duplicate symbols. This makes sense though and is not a CocoaPods problem.Drowsy
@MihaiDamian What did you end up doing regarding the duplicate symbols. Ultimately you want to pods library to be linked only to the main app target, and not the static lib as well.Sianna
This doesn't work for me. Only the last link_with statement is getting honored. So in the above example, the only target linked with Pods.a would be "sharedStuff".Bluma
@AndreyGordeev I ended up using the cocoapods-packager tool to build a framework from each pod, then I drag those into XCode. I don't use the Cocoapods integration with XCode. If your dependencies support it you could also use the newer package manager Carthage which basically takes this approach.Bluma
R
7

This is my folder structure

OB
|podfile
|Project1->Project1.xcodeproj
|Project2->Project2.xcodeproj

and this is my podfile inside the OB Folder

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'

workspace 'OB.xcworkspace'
use_frameworks!

# ignore all warnings from all pods
inhibit_all_warnings!

project 'Project1/Project1.xcodeproj'
project 'Project2/Project2.xcodeproj'

abstract_target 'OB' do
    pod 'Alamofire', '~> 4.0'

    target 'Project1' do
        project 'Project1/Project1.xcodeproj'
    end

    target 'SchoolKids' do
        project 'Project2/Project2.xcodeproj'
    end
end

This will add Afnetworking/Alamofire to both projects.If we need a exclusive pod to a particular project then we can do this

 target 'Project1' do
        project 'Project1/Project1.xcodeproj'
        pod 'Alamofire', '~> 4.0'
    end
Raquel answered 3/12, 2016 at 6:1 Comment(0)
T
2

Thanks to Rahul Vyas 's answer, I finally sucess in my project. My project is in react-native, and I use a git submodules as subproject. So this is my pod file:

workspace 'LuminPDFApp.xcworkspace'
# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'
use_frameworks!

inhibit_all_warnings!

project 'LuminPDFApp.xcodeproj'
project '../submodules/react-native-filepicker/ios/RNFilePicker.xcodeproj'

abstract_target 'LuminPDFApp' do
    pod 'SwiftyDropbox'

    target 'LuminPDFApp' do
        project 'LuminPDFApp.xcodeproj'
    end

    target 'RNFilePicker' do
        project '../submodules/react-native-filepicker/ios/RNFilePicker.xcodeproj'
    end
end
Trappist answered 23/8, 2018 at 4:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.