WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

How to use SwiftUI in Swift Playgrounds

Get started with SwiftUI right on your iPad

Paul Hudson       @twostraws

Apple just updated its Swift Playgrounds iPad app with support for Swift 5.1, dark mode, and SwiftUI, which means it’s now possible to try building SwiftUI apps and components entirely on your iPad.

Although Swift Playgrounds comes with a handful of templates for various kinds of projects, none of them are for SwiftUI – at least not yet. Hopefully that will change in the near future, not least because we’re approaching Code Week here in the EU – it would be great to see Apple taking Everyone Can Code to the next level with something based around SwiftUI.

In the meantime, if you want to get started you can create a new Blank Playground then add your code there. It takes a little boilerplate, so to save you some time here’s the code you need to get started:

import SwiftUI
import PlaygroundSupport

struct ContentView: View {
    var body: some View {
        Text("Hello World")
    }
}

PlaygroundPage.current.setLiveView(ContentView())

At this time support for SwiftUI isn’t ideal: NavigationView doesn’t work well because it tries to run as a split view controller, and there is no support for the instant preview canvas that we get with Xcode – you need to keep tapping “Run My Code”.

Still, it’s a massive leap forward, and is only going to improve. And don’t forget that even without SwiftUI you still get all the benefits of iOS 13, including the new Swift 5.1 opaque return types, Combine, and more.

If you’d like to learn more about how to use SwiftUI to build apps, you should read my free online book: SwiftUI by Example. You might also want to try my 100 Days of SwiftUI course, which teaches you SwiftUI as you build real apps.

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.5/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.