Swift.iOS: CocoaPods – Working with Targets

In this lesson

Targets! They’re everywhere! And CocoaPods can work with them! Watch to learn how to work with different targets and different types of targets using CocoaPods.

Kyle Roberts
Swift Guru at Large

Kyle's Series

Transcript

Tap on time to skip ahead

00:10

Hello world. Kyle here with Brax.tv and we’re going to talk about some of the target features in CocoaPods 1.0. Some of the stuff was present in the 1.0 betas but also some of the stuff that we’re going to be working with is just now fully supported with the 1.0 release.

00:31

To define a target in your Podfile that some pods will be installed too is just the syntax “target ‘SteamReader’ do” which is our main app target here. That’s just in single quotes wrapped in this ruby do end block. Then, listing our pods within that.

00:50

A common thing when working in Xcode is to have a unit test target. I believe my unit test target is called SteamReaderTests. We can just define that target block within the existing one and use a line like this that is inherit search paths. What this will do, since you don’t necessarily need to install all those same pods to your test targets, this includes UI test targets as well. While this does not install any pods to your test target, it is going to let you test target know that these pods do exist and where to find them. While you’re working in SteamReaderTests, I could actually be using MagicalRecord code from the MagicalRecord pod. Also, within this block of course, you can add any pod definitions that will just be for your test target. I’ve just defined and made up one here called TestingYo.

01:53

With this Podfile, we’re installing these CocoaPods to our target SteamReader. Within the target SteamReaderTests we are inheriting the search paths from above so that our test target can use these Pods if needed, or at least knows where to find them. And defining our own separate testing CocoaPods just for our test target.

02:16

Another way to work with targets is if you have separate targets for different platforms. Let’s just pretend that there’s a tvOS version of my SteamReader app right here. There is a way that we can share CocoaPods between the standard iOS iPhone SteamReader app and the tvOS SteamReader app. That is using something called an abstract target. The syntax is just abstract_target, underscore in between. The target name, you actually want to use something that is not an existing target name. My existing target name is SteamReader. I’m just going to change that to Application and add a new separate target definition under here. Currently, I don’t have a tvOS target name but let’s just call it that. I’m just making this up but what if we wanted a separate SteamReader CocoaPod called TVTool. I’m not sure if that’s a real CocoaPod at all. I’m assuming it’s not.

03:25

Another thing we want to do if we’re working with different platforms is actually move the platform definition line from the top of our Podfile to the actual target definition here. If we wanted some specifically iOS for iPhone or iPad targets, we could have a target called SteamReader iOS, wrap it in that do and end stuff. Paste the platform in there and make sure it says iOS. Even copy it’s own pods specifically to the iOS version of our app.

04:09

In this Podfile, what’s happening is, this abstract target of application is going to install the pods we have listed here to any of the subtargets that were explicitly defined below. Both the TV and iOS versions of SteamReader are going to have Alamofire, AlamofireImage, all of these installed to them.

04:31

In addition, each target, so far, in this current made up state, will each have their own little util for their platform to find. The SteamReader target will again be able to see all of these CocoaPods as well as the TVTool CocoaPod. Same with the iOS, all of the above plus the Phone Pod. Which is another made up CocoaPod that I just came up with. It’s pretty good. I’m actually thinking about getting a job just naming CocoaPods and third party resources. But for now, I’m just making videos. Thanks for watching.

Additional Info

Register to get access to additional resources and info.