Using Playground with CocoaPods
Issue #113
This is a follow up from my post Learning from Open Source: Using Playground on how to actually add a playground to your production project.
The idea is simple: create a framework so that Playground can access the code. This demo an iOS project with CocoaPods. See the demo https://github.com/onmyway133/UsingPlayground
This is also my question to this question https://stackoverflow.com/questions/47589855/how-to-expose-your-project-code-to-a-xcode-playground-when-using-cocoapods/47595120#47595120
1. Add a pod
Create a new project called UsingPlayground
. Create a Podfile
with a pod Cheers because we want something fun 😄
1 | platform :ios, '9.0' |
2. Use the pod in your project
This is very straightforward. Just to make sure the pod work
1 | import UIKit |
Build and run the project to enjoy a very fascinating confetti 🎊
3. Add a CocoaTouch framework
In your workspace, select the UsingPlayground
project, add new CocoaTouch framework. Let’s call it AppFramework
.
Then add source files to this framework. For now, just check file ViewController.swift
add add it to the AppFramework
target too.
4. Public
Swift types and methods are internal by default. So in order for them to be visible in the Playground, we need to declare them as public.
1 | public class ViewController: UIViewController { |
5. Add pod to AppFramework
In order for AppFramework to use our pods, we need to add those pods into framework target as well. Add target 'AppFramework'
to your Podfile
1 |
|
Now run pod install
again. In some rare cases, you need to run pod deintegrate
and pod install
to start from a clean slate
6. Add a Playground
Add a Playground and drag that to our workspace. Let’s call it MyPlayground
6. Enjoy
Now edit our MyPlayground
. You can import frameworks from pod and our AppFramework
1 | import UIKit |
Remember to toggle Editor Mode so you can see Playground result