installed-simulators

A small command line utility that will query the installed simulators on the given machine,
and then generate a file that can be used to easily specify previewDevice()s in SwiftUI.

Brief

installed-simulators is designed to be run as a pre-build step, so the file with all
the simulators is automatically updated on every build.

Once the file is created, you can do something like this:

// Simulator.swift, generated by this project.
enum Simulator {
    // ...
    static let iPhone13Pro = PreviewDevice(rawValue: "iPhone 13 Pro")
    // ...
}

// SomeView.swift, a part of your project.
struct SomeView: View {
    var body: some View {
        Text("Hello, world")
    }
}

struct SomeViewPreviews: PreviewProvider {
    static var previews: some View {
        SomeView()
            .previewProvider(Simulator.iPhone13Pro)
    }
}

Options

Everything below is optional; defaults are provided.

  • -h or --help: Show help
  • --export-path <export path>
    Specifies where the file should be created. If a filename is provided, it is ignored.
    Defaults to the current directory.
  • --type-name <type name>
    The name of the enum that is created; this is also the filename. Defaults to Simulator.
  • --xcrunpath <xcrun path>
    The path to xcrun. Defaults to /usr/bin/xcrun.
  • --version
    Shows the version number.

Installation in Xcode

This is the quick-and-dirty version of installation instrucitons; you may wish to make different choices, such as a different emitted filename or placing it in a different directory. That is left as an exercise for the reader.

  1. Build a binary — or get one from Releases — and put it in your project folder, peer with your xcodeproj.
  2. Open your project in Xcode
  3. Click on your project in the Project Navigator
  4. Select your target in the sidebar
  5. Click the + button

Screen_Shot_2022-01-05_at_9_29_31_AM

  1. Select New Run Script Phase
  2. Enter the following script, replacing project-dir for your project’s directory:
    $SRCROOT/installed-simulators --export-path ./project-dir
  3. Move that new Run Script phase toward the top of your list of operations; generally speaking, just below Dependencies is appropriate.
  4. Once you’ve built at least once, add the newly created file (defaults to Simulator.swift) to your project as you would any other file
  5. Now you can easily do SwiftUI previews for other devices!

GitHub

View Github