XCode ‐ Podfile with categories and multiple targets

Welcome back,

what I want to share is an ordered and readable Podfile.

The Podfile is a specification that describes the dependencies of the targets of one or more projects.

To use a Podfile you need to install a app called CocoaPods.

CocoaPods is a dependency manager for and Objective-C projects. It has over 47 thousand and is used in over 3 million apps. CocoaPods can help you scale your projects elegantly.

$ sudo gem install cocoapods

For more informations please take a look in the official website: https://cocoapods.org/

Let me show a classic example of Podfile:

platform :ios, '11.0'

 'YourApp' do
  use_frameworks!

  pod 'SwiftyJSON', '~> 2.3' 
  pod 'AFNetworking', '~> 2.6' 

end

You can also add the targets you want. If you have target or another device target.

platform :ios, '11.0'

target 'YourApp' do
  use_frameworks!

  pod 'SwiftyJSON', '~> 2.3' 
  pod 'AFNetworking', '~> 2.6' 

  target 'YourAppTests' do
    inherit! :search_paths
  end

end

Using the keyword inherit! :search_paths you tell to XCode to use all of the pods in the list in your second target.

Sometimes happens that some pod is not compatible to your target and you want to exclude.

There are two ways to do this:

  1. copy / paste all pods in all targets except the unuseds
  2. make functions (categories) using def keyword

We ignore the 1st point and go deeper to 2nd one. You have some pods used only in the test target and some others used only in the main target:

# testing pods are strictly related to the test target
def testing_pods
    pod 'Mockit', '1.4.0'
    pod 'Quick', '1.3.0'
    pod 'Nimble', '7.1.1'
    pod 'Cuckoo', '0.11.3'
end

# this other pods are used only in the main target, not the test target
def debug_tool_pods
    pod 'Crashlytics'
    pod 'Fabric'
    pod 'Instabug', '7.10.1'
    pod 'Mixpanel-swift', '2.2.3'
    pod 'Sentry', '~> 3.8'
    pod 'Firebase/Core'
end

You can import only in your test target and not in the main one.

platform :ios, '11.0'

target 'YourApp' do
  use_frameworks!

  def testing_pods
     pod 'Mockit', '1.4.0'
     pod 'Quick', '1.3.0'
     pod 'Nimble', '7.1.1'
     pod 'Cuckoo', '0.11.3'
  end

  def debug_tool_pods
     pod 'Crashlytics'
     pod 'Fabric'
     pod 'Instabug', '7.10.1'
     pod 'Mixpanel-swift', '2.2.3'
     pod 'Sentry', '~> 3.8'
     pod 'Firebase/Core'
  end

  pod 'SwiftyJSON', '~> 2.3' 
  pod 'AFNetworking', '~> 2.6' 
  debug_tool_pods  # here your debug tools

  target 'YourAppTests' do
    inherit! :search_paths
    testing_pods # here your testing tools
  end

end

In this way you use all the testing_pods in yourYourAppTests target and in the YourApp target you use only the pods in the list and the other debug_tool_pods.

BONUS: set a specific swift for a certain pod(s)

You have a pod that is not compatible with your Swift version or use a lower Swift version?

You can use the post_install functions in the Podfile like this:

post_install do |installer|
    installer.pods_project.targets.each do |target|
        if ['Mockit', 'Cuckoo'].include? target.name
            target.build_configurations.each do |config|
                config.build_settings['SWIFT_VERSION'] = '4.1'
            end
        end
    end
end
In this way your pods called Mockit and Cuckoo use the SWIFT_VERSION 4.1.

 

You can check this setting selecting your Pods project and next selecting your pods. This SWIFT_VERSION parameter can be founded in the Build Settings tab.

 

that’s all.