Include Carthage-built frameworks in your application

In this lesson

In this lesson, I will show you how to include the Carthage-built frameworks in your application so that you can import the frameworks and write code.


Tap on time to skip ahead


Hello everyone. In this lesson, we’re going to talk about how to use our Carthage-built frameworks in an application. So far, I’ve created a folder, MyNewApp, and I’ve used Carthage to check out various dependencies and also build those dependencies as frameworks. Now it’s time to create an Xcode project that we can use these dependencies in.


I’ve launched Xcode and I’m going to create a new project. I’m going to make it a single view application just to keep things simple. The product name is going to be HelloCarthage. All the defaults are going to apply. It’s going to be Swift application. I want to include unit tests and UI test targets. If I click Next, it’s going to prompt me where to put this, I’m going to put this in the MyNewApp folder. Since I’m doing this a little bit out of order, there’s going to be a little bit of housekeeping in order to clean things up a little bit.


Since normally you create your Xcode project first, it creates a folder and inside of that it creates some other folders. Since I have a little bit too much nesting here, I’m going to do a little housekeeping. I’m going to call this temp for now and drag all the internal folders of this project out into the MyNewApp folder and then I don’t need this anything. That’s all there is to it.


Now that I have my project structured as I want, I want the Cartfile and the Cartfile.resolved in the same folder as the project. Let’s go ahead and get started.


Let’s go ahead and open up Xcode. I’ve opened the main storyboard. The first thing that I want to do is to add a label just to say Hello World. Let’s put it approximately in the middle and say, “Hello Carthage.” We want to make sure that stays centered. We’re going to click this and add a couple of layout constraints. Go ahead and update the frames to fix this. Now we’re ready.


I now have a Hello Carthage label and we’re ready to launch the app and see it run. Let’s go ahead and click run. We’re going to run the app on the simulator and just verify that everything is set up correctly and there we have our new label.


Now it’s time to start referencing some of our frameworks that we’ve pulled in from Carthage. I’m going to write a little bit of code. Let’s head over to the AppDelegate. The first thing I want to do is import one of my dependencies, SwiftyBeaver. What? It can’t find it. Well, I have all of the dependencies declared and they’re in the Carthage Build folder but I can’t seem to reference those. Let’s see what we do to resolve that problem.


In order to resolve that, I’m going to open up the Build Settings here and let me make a little room. I’m going to look at the General tab of the main target. The first thing we’re going to notice is that there are no linked frameworks or libraries. This should be pretty easy to fix.


I’m going to locate the SwiftyBeaver, one of my dependencies, one of those frameworks. Scrolling down through here you’ll notice, well, it’s not visible anywhere. I’m going to have to click Add Other. This is going to open up a Finder and allow me to navigate into Carthage Build and I’m going to go look for the SwiftyBeaver framework. I’m going to go ahead and add that. Now you’ll notice that SwiftyBeaver is added to the linked frameworks and libraries. If I go back, I should now be able to find it. I can now- there it is- I can now run that. Or I should say reference it. I can now reference it in an import statement.


Now that we know how to do that, let’s go back to the targets and we want to add all of the frameworks that we have brought in with Carthage. I’m going to click Add Other. I’m just going to go through and add them all. Because I wouldn’t have referenced them had I not wanted to be able to use them somehow.


I need to pull in every single framework except for the two in the private. I’m going to pull in all these RxCocoa frameworks. Anything having to do with testing we’re going to leave out for now. All of these swift-protocols. I already have SwiftyBeaver and Swinject. Now we have all of our frameworks pulled in through Carthage referenced in the Linked Frameworks and Libraries section of our project and now we can write code against that. That’s how you reference the libraries that you pulled in from Carthage.