Running an app that contains Carthage-built frameworks

In this lesson

In this lesson I will show you how to run your application on device or on the simulator by making sure all of your Carthage-built frameworks get copied into the bundle.


Tap on time to skip ahead


In this lesson, I’m going to talk about another problem that we’re going to encounter. But, before I describe what that problem is, let me show you that we’ve imported into our project all of the dependencies that we’ve declared in Carthage.


I’m going to navigate to the AppDelegate, because I want to start writing some code. The first I’m going to do is say let log = SwiftyBeaver.self just so we give it a bit more obvious name.


And then I’m going to paste in some code. It doesn’t really matter what this does. The point is that I’m using Swifty Beaver, one of the dependencies that I declared, in order to add some logging to my application.


Let’s run this code. I already saw that I could import the dependency. Let me pull up the simulator and wait for it to launch, and well it crashed. So, what’s happened here? Let’s take a look at the log here and we can see that we’ve got some libraries that we’ve referenced that cannot be loaded.


This is the problem I was referring to. We added the libraries to our Build Settings so that they’re linked. The problem is that doesn’t add them to the target..the bundle, so that we can run it on device. I’m going to tell you now how we’ll solve that problem.


In order to resolve that problem where it can’t find the actual frameworks that I’ve included, I’m going to head to the Build Phases tab and I’m going to Add a New Run Script Phase. This is wanting me to run something and it’s wanting me to paste in a script here. But, exactly what do I put? 


Before I type in the script, I want to go back to the command line and show you something. I’m in the directory of MyNewApp and I’m going to type carthage help just to list out the commands I can run. This one here, copy-frameworks is one of the commands we want to run. It says, “In a Run Script build phase, copies each framework specified by a SCRIPT_INPUT_FILE environment variable into the built app bundle”.


That’s exactly what we want to use, so let’s head back to XCode and do that. What I want to do is specify /usr/local/bin/carthage copy-frameworks. That’s going to run this, and the thing that I need to provide it is a list of every single one of those frameworks. I’m going to put Carthage/Build/iOS and I’m going to list every single framework.


The one that’s missing here is AlamoFire.framework. I’m going to have to repeat this for every single framework that I want to be part of my application.  I’m not going to make you watch while I do that. We’ll come back when I’ve typed all of that in.


I’ve now specified all of the frameworks as part of the copy-frameworks shell script. I’m using the copy-frameworks command and as input command, all of the that I want to copy.


Let me see if that runs. I’m going to go ahead and run it and hopefully it will actually run now since I’m copying all of the frameworks into the bundle. Let me pull up the simulator so we can see it launch. “Build Succeeded”, and it launches and there is my label. I have my log statement “Hello Carthage” that I specified right here.


That’s how you include libraries or frameworks that were built in Carthage. You not only have to add them to the Linked Build Settings, you also need to add a copy-files phase so that they get copied into the bundle.

Additional Info

Register to get access to additional resources and info.