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 theenum
that is created; this is also the filename. Defaults toSimulator
.--xcrunpath <xcrun path>
The path toxcrun
. 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.
- Build a binary — or get one from Releases — and put it in your project folder, peer with your
xcodeproj
. - Open your project in Xcode
- Click on your project in the Project Navigator
- Select your target in the sidebar
- Click the
+
button
- Select
New Run Script Phase
- Enter the following script, replacing
project-dir
for your project’s directory:
$SRCROOT/installed-simulators --export-path ./project-dir
- Move that new
Run Script
phase toward the top of your list of operations; generally speaking, just belowDependencies
is appropriate. - 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 - Now you can easily do SwiftUI previews for other devices!