onmyway133 / blog

🍁 What you don't know is what you haven't learned
https://onmyway133.com/
MIT License
681 stars 33 forks source link

Using Playground with CocoaPods #113

Open onmyway133 opened 7 years ago

onmyway133 commented 7 years ago

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.

framework

Then add source files to this framework. For now, just check file ViewController.swift add add it to the AppFramework target too.

targets

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

play

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

enjoy

fespinoza commented 7 years ago

Great tutorial @onmyway133, very detailed, thank you very much. 🎊

I tried your solution and it works all good πŸ‘

festrs commented 6 years ago

My playground wont recognise the appframework any clue?

fespinoza commented 6 years ago

I recently had a discovery about this:

when adding importing a custom framework to a playground, it won’t recognize the module until you build the framework towards a simulator, not an actual device (air development in this case) 🀯

onmyway133 commented 6 years ago

@fespinoza @festrs I think I will write a proper article for this on Medium

richardtop commented 6 years ago

Hmm, can't add a framework in Xcode 9.2, there is no such a menu item.

image

However, I can import CocoaPods without creating a custom framework for the app, which is enough to prototype edge-cases.

onmyway133 commented 6 years ago

@richardtop Hi, don't know what happen to you, but I can see that option in both Xcode 9.2 and 9.3. Maybe you can try force closing Xcode and start again

framework

richardtop commented 6 years ago

@onmyway133 I was creating a file, not a new project.

onmyway133 commented 6 years ago

@fespinoza @richardtop @festrs as promised, here is the article https://medium.com/flawless-app-stories/playground-driven-development-in-swift-cf167489fe7b .Hope you find it useful ❀️

richardtop commented 6 years ago

@onmyway133 thanks, but I think, I'm going for ReactNative for data-driven apps... The developer experience is so much faster than 🍎

artemnovichkov commented 6 years ago

Hi, @onmyway133! I have made an example with Carthage https://github.com/artemnovichkov/UsingPlayground

fespinoza commented 6 years ago

Nice post @onmyway133! very detailed

i liked the iron man example. Iron man + playgrounds = ❀️

I am creating this repo where i put playground specific tips https://github.com/fespinoza/HowToPlayground

twhitt14 commented 6 years ago

I feel like a step was skipped when adding the CocoaTouch Framework - do I need to select the workspace or project under the "Add to:" option? After following the instructions my compiler is still mad and can't see the frameworks correctly.

onmyway133 commented 6 years ago

@twhitt14 Hi without seeing your project it's hard to say what's wrong. Mind making a minimal project that shows what's is not working for you?

johngrayau commented 5 years ago

I recently had a discovery about this:

when adding importing a custom framework to a playground, it won’t recognize the module until you build the framework towards a simulator, not an actual device (air development in this case) 🀯

@fespinoza THANK YOU THANKYOU THANKYOU SO MUCH!!!! I spent 8 hours reading every tutorial on the web and trying every Scheme combination across 3 trial projects - one of which worked then mysteriously stopped, until I saw your quote and was able to make the all work in under 3 minutes!!!

amberkatyal commented 4 years ago

Just out of curosity. It works for the above steps, I was ambiguous at one of steps when creating a framework. You have to specify this check: "Embed in Application" Screenshot 2020-01-17 at 3 00 18 PM If you set it as none. Playground will throw you error no such module. And even cocoapods will say: The Podfile contains framework or static library targets (AppFramework), for which the Podfile does not contain host targets (targets which embed the framework). If this project is for doing framework development, you can ignore this message. Otherwise, add a target to the Podfile that embeds these frameworks to make this message go away (e.g. a test target).

So what does embed in application means here. Are we embedding this in our application? .ipa file.