In this post, I’m going to show off how to use 3rd party libraries and frameworks imported with CocoaPods in Xcode 9 playgrounds. This takes only a few minutes to set up.
Why would you use a framework in a playground? Glad you ask, you use Playground to prototype and test features quickly as it runs instantly… surprise, surprise. The framework part comes in handy when the sad feature is dependent on an external framework. The inspiration comes from trying to set up JTAppleCalendar in my project and I needed to constantly run the project to see the changes every time. Playground is a big time saver.
Setting up the Xcode project
If you already have a project with working libraries or frameworks I’d recommend you skip this step and use the Workspace of the project.
Creating an Xcode Project
Otherwise, go ahead and create a new Project, I’m going to create a Single View app, but it shouldn’t matter which template you use. I’ll name my playDemo.
- Open terminal and run the following commands
$ cd <projectDirectory>
$ pod init
$ open Podfile
- Now you should have the Podfile open in Text Edit and this is where you can add your pods, I’ll ad JTAppleCalendar like so:
- Now save and close the Podfile and back in terminal run the following command
$ pod install
$ open <projectName>.xcworkspacealternatively, you can open the workspace file in the project’s directory
That is it for the setup, now you have a fully functioning project with a framework.
Set up Playground file
- Let’s start with creating the playground file, there are multiple ways to do this:
- From the Xcode Home screen
- ⌘ + ⌥ + ⇧ + N
- Name it anything and save it anywhere. I’ll leave the default name and I’ll save it next to the project directory.
Copy the Playground into the workspace
- You should already have the workspace open but if you don’t just open it from the project’s library
- File → Add Files to “<projectName>”
- Select the playground file
Finally, run the project
When running the project, make sure that you select a simulated device.
So now you can import the framework into the playground and you can start prototyping.
Hope, you enjoyed this tutorial, leave questions, recommendation and comments below and check back in a week for the latest post.