Xcode Playgrounds is a great way to rapidly prototype and run an isolated piece of Swift code: an algorithm implementation, type architecture, or even an interactive view hierarchy – with support for live views. The iterative nature of playgrounds enables experimenting with and fixing issues quickly, but sometimes it may not be clear what actually causes an undesired behaviour or an incorrect layout. Traditionally developers rely on debuggers to examine the runtime state of their program at a particular point in time without having to modify and restart it, but aside from timeline previews, this sort of functionality is not currently available in playgrounds. This includes the View Debugger, which means that diagnosing obscure layout issues in custom live views requires resorting to trial and error… unless of course you have Reveal!
With just a few steps you can integrate the support library into your iOS or tvOS playground and inspect live views using Reveal 2 or newer:
- Download this template playground (compatible with Xcode 8 or newer).
- If you would like to enable Reveal on an existing playground that you have, copy the code from this template (including, most importantly,
RevealServer.swiftauxiliary source) into your playground and reopen it. Otherwise, you can simply rename the template and use it as a starting point.
- In Reveal, use Help → Show Reveal Library in Finder menu to open the location of
RevealServer.frameworkcorresponding to the platform you’re targeting.
- Copy this framework to a location accessible by the playground, which is either:
- Playground’s own
Resourcesgroup in its Project navigator, or
~/Documents/Shared Playground Datafolder – this option allows you to skip this step for other playgrounds next time.
- Playground’s own
And that’s it! Run your playground in Xcode, and once the live view becomes visible, it should also appear in Reveal as if it was an app running in Simulator. The comments in the template code provide a bit more information, and can be removed whenever you like.
How does it work?
Xcode Playgrounds with live views indeed run in an automatically managed Simulator instance. Xcode generates and installs a stub application into this Simulator and uses it as a host. What you see as an interactive live view in Xcode is actually a framed “window” into the Simulator screen.
Reveal, on the other hand, communicates with the inspected app via the local network. Dynamically loading Reveal Server framework into the playground’s process enables this communication, and that’s exactly what
The framework itself, however, needs to be placed in one of the predefined locations due to playgrounds’ sandboxing rules: they’re stricter than for normal Simulators, and don’t even allow reading (let alone executing) from arbitrary locations on your machine, including
/Applications, where you’d usually have Reveal installed. Placing the framework in playgrounds’
Resources is a straightforward option, and allows sharing Reveal-instrumented playground more easily, but at a price of increased size. Taking advantage of
Shared Playground Data folder, on the other hand, may be more convenient when instrumenting multiple playgrounds, but the framework needs to be placed in that location on every machine where the playground would be inspected. Fortunately, the line of code that attempts loading the framework can be easily changed to fail silently in order to allow the playground to run without Reveal support.
Caveats and limitations
Because the Reveal support library needs to be provided to the playground explicitly, there are limitations on how much of the integration can be automated:
- Whenever you update Reveal, you’ll likely need to manually update
RevealServer.frameworkin your playgrounds’
Shared Playground Datafolder. Otherwise, the version mismatch would prevent Reveal from communicating with the playground.
RevealServer.frameworkversions for iOS and tvOS are different, which means that you’ll need to manually swap it if you inspect playgrounds of both platforms and use
Shared Playground Dataas the framework location.
- And finally, Swift Playgrounds on iPad are unfortunately not supported by this integration method due to even stricter sandboxing (or, more precisely, binary code signing) rules.
We hope that using Reveal in Xcode Playgrounds will make experimenting with live views and layout even faster and easier for you than before. If you have feedback or suggestions about this use case, please let us know on our support forum. Happy playing!