Build a Mac app and run it on a macOS instance
This guide shows you how to build a Mac app on an Xcode sandbox and have every build install and launch on a macOS instance. After the first install, each build sends only the files that changed. Signing the app for distribution and running its tests are not covered.
Before you start
You need the following:
- API key: set
LIM_API_KEY. - CLI or SDK:
lim0.65.3 or later, or@limrun/api0.79.4 or later. To update the CLI, runnpm install --global lim@latest. - Your app: the build is signed to run locally, without a certificate or provisioning profile. Capabilities that need a provisioning profile, such as iCloud and push notifications, are not available to it.
For how the Xcode sandbox syncs and builds your project, see Build with Xcode.
Create the instances
Create a macOS instance to run the app on. The command waits until the virtual machine is ready, then prints its ID and console URL:
lim macos create --inactivity-timeout 30mFor the other create flags, such as a jurisdiction or labels, see Run a macOS VM.
Create an Xcode sandbox to build on:
lim xcode createAttach the macOS instance
Attach the macOS instance to the Xcode sandbox, so the sandbox installs every Mac build on it:
lim xcode attach-macos <macos-instance-id>import Limrun from '@limrun/api';
const lim = new Limrun(); // Reads LIM_API_KEY.
const xcode = await lim.xcodeInstances.createClient({
instance: await lim.xcodeInstances.get('<xcode-instance-id>'),
});
await xcode.attachMacOS(await lim.macosInstances.get('<macos-instance-id>'));api := limrun.NewClient() // Reads LIM_API_KEY.
sandbox, err := api.XcodeInstances.Get(ctx, "<xcode-instance-id>")
if err != nil {
return err
}
mac, err := api.MacosInstances.Get(ctx, "<macos-instance-id>")
if err != nil {
return err
}
xcode, err := api.XcodeInstances.CreateClient(*sandbox)
if err != nil {
return err
}
if _, err := xcode.AttachMacOS(ctx, mac.Status.APIURL, mac.Status.Token); err != nil {
return err
}If you omit the ID, the CLI attaches the last macOS instance you created in the current workspace. It attaches to the most recent Xcode sandbox there unless you pass --id <xcode-instance-id>.
In Python, attach with an HTTP call: send POST {xcode status.apiUrl}/macos with the Xcode sandbox's status.token as the bearer token and the body {"apiUrl": "<macOS status.apiUrl>", "token": "<macOS status.token>"}.
Attaching installs nothing yet. The next successful build is the first one the macOS instance gets.
Build and run the app
From your project's directory, build with the macosx SDK:
lim xcode build --sdk macosxOnce the app is installed, the build log shows Installed to the macOS instance in <duration>. The macOS instance installs the app in ~/Applications and launches it. To use the app, open the instance's console URL; lim macos get prints it again.
While a macOS instance is attached and no simulator is, macosx is the default SDK, so lim xcode build alone builds the Mac app.
Each later build replaces the installed app. If the app is running, the macOS instance stops it and launches the new build.
To read what the app logs, follow its log on the macOS instance by its bundle ID:
lim macos app-log com.example.MyMacApp --followTo pass launch arguments or environment variables, relaunch the installed app with them, for example lim macos launch-app com.example.MyMacApp --mode RelaunchIfRunning --env API_URL=https://staging.example.com. Run a macOS VM covers app logs, launching, and the other commands that work on the instance.
Reload Swift edits without relaunching
To see a Swift edit in the running app without losing its state, build with hot reload:
lim xcode build --sdk macosx --hot-reload
# edit a Swift file, then build again
lim xcode build --sdk macosx --hot-reloadawait xcode.xcodebuild({ sdk: 'macosx' }, { hotReload: true });The first hot reload build installs and launches a Debug build as usual. When a later hot reload build changed only Swift code, the running app shows the edit as the build ends and keeps its state. Any other change installs and launches the app again, and the build output says which happened. Hot reload is for Debug builds only and can't be combined with signing flags or an App Store upload. In Go, set HotReload on the build request. Hot reload needs @limrun/api 0.80.0, Go SDK v0.15.0, or lim 0.66.0.
Detach the macOS instance
To stop installing builds on the macOS instance, detach it from the sandbox. Both instances and the installed app remain:
lim xcode detach-macos --id <xcode-instance-id>Develop a Mac app and try every build on a macOS instance, with no Mac of your own.
Was this guide helpful?