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 😄
platform :ios, '9.0'
use_frameworks!
pod 'Cheers'
target 'UsingPlayground'
2. Use the pod in your project
This is very straightforward. Just to make sure the pod work
import UIKit
import Cheers
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let cheerView = CheerView()
view.addSubview(cheerView)
cheerView.frame = view.bounds
// Configure
cheerView.config.particle = .confetti
// Start
cheerView.start()
}
}
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.
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
platform :ios, '9.0'
use_frameworks!
pod 'Cheers'
target 'UsingPlayground'
target 'AppFramework'
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
import UIKit
import Cheers
import AppFramework
import PlaygroundSupport
let cheerView = CheerView()
cheerView.frame = CGRect(x: 0, y: 50, width: 200, height: 400)
// Configure
cheerView.config.particle = .confetti
// Start
cheerView.start()
let myController = ViewController()
PlaygroundPage.current.liveView = myController.view
Remember to toggle Editor Mode so you can see Playground result