zixun zixun - 20 days ago 7
Objective-C Question

how to share the some pods in two target by podfile

xcodeproj 'Demo.xcodeproj'

platform :ios, "6.0"

target "Demo" do
pod 'Demo-A', '3.1.1'
pod 'Demo-B', '1.0'
pod 'Demo-C', '~> 1.9.0'
pod 'Demo-D', '~> 1.1'

pod 'AA', '0.0.1'
pod 'BB', '0.0.1'
pod 'CC', '0.0.1'
pod 'DD', '0.0.1'
pod 'EE', '0.0.1'
pod 'FF', '0.0.1'
pod 'GG', '0.0.1'
end

target "DemoTests" do
pod 'DemoTests-X', '3.1.1'
pod 'DemoTests-Y', '0.4.0'
pod 'DemoTests-Z', '4.1.1'

pod 'AA', '0.0.1'
pod 'BB', '0.0.1'
pod 'CC', '0.0.1'
pod 'DD', '0.0.1'
pod 'EE', '0.0.1'
pod 'FF', '0.0.1'
pod 'GG', '0.0.1'
end

target "DemoWidget" do
pod 'DemoWidget-1', '3.1.1'
pod 'DemoWidget-2', '0.4.0'
pod 'DemoWidget-3', '4.1.1'
end


this is my podfile . as u can see.

pod 'AA', '0.0.1'
pod 'BB', '0.0.1'
pod 'CC', '0.0.1'
pod 'DD', '0.0.1'
pod 'EE', '0.0.1'
pod 'FF', '0.0.1'
pod 'GG', '0.0.1'


this code is appear twice. DemoTests is test target for Demo target.while i write test file and improt the source file in Demo target.it will dependence some file in pods which in Demo target,but not in DemoTests target. so i write this Podfile code twice for dependence.
but,it's ugly! my colleagues and i have to maintain two sets of such code.and my Podfile will be Bloated´╝ü
i want to maintain this code once! how to do it~
I love cocoapods! i believe it will solve my problem ~
waiting for u answer~~~~

Answer

You can use it like this to reduce your length of podfile and would also be able to maintain code once!!!

xcodeproj 'Demo.xcodeproj'

platform :ios, "6.0"

def common_pods_for_target
    pod 'AA', '0.0.1'
    pod 'BB', '0.0.1'
    pod 'CC', '0.0.1'
    pod 'DD', '0.0.1'
    pod 'EE', '0.0.1'
    pod 'FF', '0.0.1'
    pod 'GG', '0.0.1'
end

target "Demo" do
    pod 'Demo-A', '3.1.1'
    pod 'Demo-B', '1.0'
    pod 'Demo-C', '~> 1.9.0'
    pod 'Demo-D', '~> 1.1'
    common_pods_for_target
end

target "DemoTests" do
    pod 'DemoTests-X', '3.1.1'
    pod 'DemoTests-Y', '0.4.0'
    pod 'DemoTests-Z', '4.1.1'
    common_pods_for_target
end

target "DemoWidget" do
    pod 'DemoWidget-1', '3.1.1'
    pod 'DemoWidget-2', '0.4.0'
    pod 'DemoWidget-3', '4.1.1'
end

Just define all your common pods in a variable and use it in the targets you want.