Using 3rd party frameworks in Xcode 10 Playgrounds

Paul Ardeleanu
4 min readAug 10, 2018

--

For updated instructions (Swift 5 and Xcode 11), please visit: https://www.pardel.net/blog/2019/swift-3rdparty-playgrounds

Xcode Playgrounds is a great place to prototype code and even practice Test-Driven Development. And sometimes, 3rd party libraries might be handy to use. In this article, I’m gonna show you two ways to use Xcode Playgrounds with frameworks.

The problem we’re trying to solve

3rd party frameworks can’t simply be included in a playground, not even as part of “Sources”:

I. The Simple Way (for when using a single framework)

Say we have a framework that we want to test in a playground — for the example below, I’ve picked a well known library: SwiftSpinner written by the prolific open-source(re)r and author Marin Todorov.

The downloaded archive contains Xcode project that can be opened directly. Make sure the project builds successfully (Cmd+B)

The Playground

Let’s now create the playground that’s we’re going to use — from the menu select File > New > Playground, make sure it’s an iOS playground, and select the ‘Single View’ template.

You can use the default name (MyPlayground.playground) and store it inside the framework itself:

Once the playground is created, switch to the Assistant Editor to view the playground output:

Using the framework is pretty easy now:

  1. Import it at the top:

2. Use the spinner in the willdidAppear method:

And here is the result:

II. Using Multiple Frameworks

Let’s see how we can use multiple frameworks in a playground. We’re going to use the same SwiftSpinner and also Alamofile.

To start, download both libraries and store them side by side in a folder:

Now, we’ll open the SwiftSpinner Xcode project as before and we’re going to create a workspace around it — from the menu select: File > Save as Workspace. I’ve named mine: Playground.workspace and saved it beside the two folders:

Adding the Second Framework

Let’s now add the second framework — from the Alamofire-master project, drag and drop Alamofire.xcodeproj (make sure you add the project and not the Alamofire.xcworkspace) onto the workspace beside the SwiftSpinner project.

The workspace should now contain the schemes from both frameworks.

The Playground

Just as we did in the previous section, we’ll create a playground — from the menu select File > New > Playground, make sure it’s an iOS playground, and select the ‘Single View’ template. Make sure it is added to the Playground group.

One Scheme to Rule Them All

Now, before we can use the frameworks, we’ll have to build them one by one. Whilst it’s not that hard in our case, it can be a nuisance when using a bigger number of them or if you actively develop them whilst using with a playground. For this, we’ll add a new scheme that will build all frameworks.

From the Scheme picker, select “New Scheme…”:

Make sure the target is “None” — I’ve named mine Playground:

On the next screen, under the Build category, use the little + button to add the dependent targets — here is how it should be configured:

Click “Close”. Now, every time you build the Playground target, all the dependents will be also built.

NB: Make sure you’re building for a Simulator and not a real device.

Using the Frameworks

The frameworks can now be used as before:

  1. Import them

2. Use the spinner whilst a network call is in progress

When the playground is run, you’ll now see the spinner briefly and the content of the HTTP response in the console:

That’s it!

Happy Playgrounding! 🤓

--

--

Paul Ardeleanu

iOS & Web engineer, trainer and coach - with a dash of UX & UI.