### Install an Application with SimulatorDevice.installApp(from:) Source: https://context7.com/vmanot/simulatorkit/llms.txt Installs an iOS application bundle (.app) onto the simulator device using `xcrun simctl install`. The simulator must be booted before installing apps. Provide the file URL to the .app bundle. ```swift import SimulatorKit import Foundation Task { do { let devices = try await SimulatorDevice.all() guard let device = devices.first(where: { $0.name.contains("iPhone") && $0.state == .booted }) else { print("No booted iPhone simulator found") return } // Path to the .app bundle (typically from Xcode build output) let appURL = URL(fileURLWithPath: "/path/to/MyApp.app") print("Installing app on \(device.name)...") try await device.installApp(from: appURL) print("App installed successfully") } catch { print("Failed to install app: \(error)") } } ``` -------------------------------- ### SimulatorDevice.boot() Source: https://context7.com/vmanot/simulatorkit/llms.txt Starts the specified simulator device if it is not already booted. ```APIDOC ## SimulatorDevice.boot() ### Description Starts the specified simulator device if it is not already booted. This async function executes xcrun simctl boot with the device's UUID. If the device is already in the booted state, the function returns immediately without error. ``` -------------------------------- ### SimulatorDevice.installApp(from:) Source: https://context7.com/vmanot/simulatorkit/llms.txt Installs an iOS application bundle (.app) onto the simulator device. ```APIDOC ## SimulatorDevice.installApp(from:) ### Description Installs an iOS application bundle (.app) onto the simulator device. This async function uses xcrun simctl install to copy the app from the specified file URL to the simulator. The simulator must be booted before installing apps. ### Parameters - **from** (URL) - Required - The file URL pointing to the .app bundle. ``` -------------------------------- ### Boot a Simulator with SimulatorDevice.boot() Source: https://context7.com/vmanot/simulatorkit/llms.txt Starts the specified simulator device if it is not already booted. This async function executes `xcrun simctl boot` with the device's UUID. If the device is already booted, it returns immediately. Ensure the simulator is booted before performing operations that require it. ```swift import SimulatorKit Task { do { let devices = try await SimulatorDevice.all() // Find an iPhone 14 Pro simulator guard let iPhone = devices.first(where: { $0.name.contains("iPhone 14 Pro") }) else { print("iPhone 14 Pro simulator not found") return } // Check current state before booting if iPhone.state == .shutdown { print("Booting \(iPhone.name)...") try await iPhone.boot() print("Simulator booted successfully") } else if iPhone.state == .booted { print("Simulator is already booted") } } catch { print("Failed to boot simulator: \(error)") } } ``` -------------------------------- ### SimulatorDevice.launchApp(withIdentifier:) Source: https://context7.com/vmanot/simulatorkit/llms.txt Launches an installed application on a booted simulator device using its bundle identifier. ```APIDOC ## SimulatorDevice.launchApp(withIdentifier:) ### Description Launches an application that is already installed on the simulator device using its bundle identifier. This async function executes `xcrun simctl launch` to start the app. The app must be previously installed on the device. ### Method `async` ### Endpoint N/A (This is a method call on a `SimulatorDevice` object) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```swift import SimulatorKit Task { do { let devices = try await SimulatorDevice.all() guard let device = devices.first(where: { $0.name.contains("iPhone") && $0.state == .booted }) else { print("No booted iPhone simulator found") return } // Launch app by bundle identifier let bundleIdentifier = "com.example.myapp" print("Launching \(bundleIdentifier) on \(device.name)...") try await device.launchApp(withIdentifier: bundleIdentifier) print("App launched successfully") // Launch built-in apps try await device.launchApp(withIdentifier: "com.apple.mobilesafari") try await device.launchApp(withIdentifier: "com.apple.Preferences") } catch { print("Failed to launch app: \(error)") } } ``` ### Response #### Success Response None (The function returns `Void` upon successful launch) #### Response Example N/A ``` -------------------------------- ### Complete Simulator Automation Workflow Source: https://context7.com/vmanot/simulatorkit/llms.txt This Swift code demonstrates a full automation workflow. It finds a specific iPhone simulator, boots it if necessary, brings it to the foreground, installs an application, launches it, waits for it to load, and then captures a screenshot, saving it to a specified path. Error handling is included for each step. ```swift import SimulatorKit import Foundation @main struct SimulatorAutomation { static func main() async { do { // Step 1: Find an iPhone simulator let devices = try await SimulatorDevice.all() guard let device = devices.first(where: { $0.name.contains("iPhone 14") }) else { print("No iPhone 14 simulator found") return } print("Using simulator: \(device.description)") // Step 2: Boot if needed if device.state != .booted { print("Booting simulator...") try await device.boot() } // Step 3: Bring to foreground try await device.foreground() // Step 4: Install application let appPath = URL(fileURLWithPath: "build/MyApp.app") if FileManager.default.fileExists(atPath: appPath.path) { print("Installing app...") try await device.installApp(from: appPath) } // Step 5: Launch app print("Launching app...") try await device.launchApp(withIdentifier: "com.example.myapp") // Step 6: Wait for app to load, then screenshot try await Task.sleep(nanoseconds: 3_000_000_000) // 3 seconds print("Taking screenshot...") let screenshot = try await device.screenshot() let outputPath = URL(fileURLWithPath: "screenshots/\(device.name).jpg") try FileManager.default.createDirectory( at: outputPath.deletingLastPathComponent(), withIntermediateDirectories: true ) try screenshot.write(to: outputPath) print("Complete! Screenshot saved to \(outputPath.path)") } catch { print("Automation failed: \(error)") } } } ``` -------------------------------- ### Launch an Application on a Simulator Source: https://context7.com/vmanot/simulatorkit/llms.txt Uses the bundle identifier to launch an installed application on a booted simulator. The target device must be in a booted state before execution. ```swift import SimulatorKit Task { do { let devices = try await SimulatorDevice.all() guard let device = devices.first(where: { $0.name.contains("iPhone") && $0.state == .booted }) else { print("No booted iPhone simulator found") return } // Launch app by bundle identifier let bundleIdentifier = "com.example.myapp" print("Launching \(bundleIdentifier) on \(device.name)...") try await device.launchApp(withIdentifier: bundleIdentifier) print("App launched successfully") // Launch built-in apps try await device.launchApp(withIdentifier: "com.apple.mobilesafari") try await device.launchApp(withIdentifier: "com.apple.Preferences") } catch { print("Failed to launch app: \(error)") } } ``` -------------------------------- ### List All Available Simulators with SimulatorDevice.all() Source: https://context7.com/vmanot/simulatorkit/llms.txt Retrieves a list of all simulator devices. This async function returns an array of SimulatorDevice structs. The function dynamically loads CoreSimulator from Xcode's private frameworks. Use this to get an overview of available simulators and their states. ```swift import SimulatorKit // List all available simulator devices Task { do { let devices = try await SimulatorDevice.all() for device in devices { print("Device: \(device.name)") print(" ID: \(device.id)") print(" Runtime: \(device.runtimeVersion ?? "unknown")") print(" State: \(device.state)") } // Filter to find specific device types let iPhoneDevices = devices.filter { $0.name.contains("iPhone") } let iPadDevices = devices.filter { $0.name.contains("iPad") } print("Found \(iPhoneDevices.count) iPhone simulators") print("Found \(iPadDevices.count) iPad simulators") } catch { print("Failed to list simulators: \(error)") } } ``` -------------------------------- ### SimulatorDevice.foreground() Source: https://context7.com/vmanot/simulatorkit/llms.txt Launches the Simulator.app and brings the specified device to the foreground. ```APIDOC ## SimulatorDevice.foreground() ### Description Launches the Simulator.app and brings the specified device to the foreground as the active device. This opens the visual simulator window, making it visible to the user for interaction or observation. ``` -------------------------------- ### Bring Simulator to Foreground with SimulatorDevice.foreground() Source: https://context7.com/vmanot/simulatorkit/llms.txt Launches the Simulator.app and brings the specified device to the foreground. This opens the visual simulator window for user interaction. The device should be booted before attempting to bring it to the foreground. ```swift import SimulatorKit Task { do { let devices = try await SimulatorDevice.all() guard let device = devices.first(where: { $0.name.contains("iPhone") }) else { print("No iPhone simulator found") return } // Boot the device first if needed if device.state == .shutdown { try await device.boot() } // Bring the simulator window to the foreground try await device.foreground() print("Simulator \(device.name) is now visible") } catch { print("Failed to foreground simulator: \(error)") } } ``` -------------------------------- ### Boot a Simulator Source: https://github.com/vmanot/simulatorkit/blob/master/README.md Boots a specific simulator device. This function is synchronous and may throw an error if the simulator cannot be booted. It requires finding a simulator instance first. ```swift import SimulatorKit let iphoneSimulator = try SimulatorDevice.all().first(where: { $0.name.contains("iPhone") })! try iphoneSimulator.boot() ``` -------------------------------- ### SimulatorDevice.all() Source: https://context7.com/vmanot/simulatorkit/llms.txt Retrieves a list of all available simulator devices from Xcode. ```APIDOC ## SimulatorDevice.all() ### Description Retrieves a list of all simulator devices available in Xcode. This async function returns an array of SimulatorDevice structs containing device information including UUID, name, runtime version, and current state. ### Method Async Function ### Response - **Array** - A list of available simulator devices. ``` -------------------------------- ### List All Available Simulators Source: https://github.com/vmanot/simulatorkit/blob/master/README.md Use this function to retrieve a list of all available simulator devices. Ensure SimulatorKit is imported. ```swift import SimulatorKit print(try! SimulatorDevice.all()) ``` -------------------------------- ### SimulatorDevice.screenshot() Source: https://context7.com/vmanot/simulatorkit/llms.txt Captures a screenshot of the simulator's current screen and returns it as JPEG image data. ```APIDOC ## SimulatorDevice.screenshot() ### Description Captures a screenshot of the simulator's current screen and returns it as JPEG image data. This async function uses `xcrun simctl io screenshot` to capture the display to a temporary file, then reads and returns the data. Note that screenshots are taken immediately, so allow time for UI to settle after app launch. ### Method `async` ### Endpoint N/A (This is a method call on a `SimulatorDevice` object) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```swift import SimulatorKit import Foundation Task { do { let devices = try await SimulatorDevice.all() guard let device = devices.first(where: { $0.name.contains("iPhone") && $0.state == .booted }) else { print("No booted iPhone simulator found") return } // Take a screenshot print("Capturing screenshot of \(device.name)...") let screenshotData: Data = try await device.screenshot() // Save to file let outputURL = URL(fileURLWithPath: "/tmp/simulator_screenshot.jpg") try screenshotData.write(to: outputURL) print("Screenshot saved to \(outputURL.path)") print("Image size: \(screenshotData.count) bytes") } catch { print("Failed to capture screenshot: \(error)") } } ``` ### Response #### Success Response (200) - **screenshotData** (Data) - JPEG image data of the simulator's screen. #### Response Example ```json { "example": "" } ``` ``` -------------------------------- ### Take a Screenshot of a Simulator Source: https://github.com/vmanot/simulatorkit/blob/master/README.md Captures a screenshot of the currently running simulator. The screenshot is returned as Data in JPEG format. Ensure a simulator is booted and accessible. ```swift import SimulatorKit let iphoneSimulator = try SimulatorDevice.all().first(where: { $0.name.contains("iPhone") })! let jpgDataOfScreenshot: Data = try iphoneSimulator.screenshot() ``` -------------------------------- ### Capture Simulator Screenshot Source: https://context7.com/vmanot/simulatorkit/llms.txt Captures the current simulator display as JPEG data. Ensure the UI has finished rendering before triggering the capture. ```swift import SimulatorKit import Foundation Task { do { let devices = try await SimulatorDevice.all() guard let device = devices.first(where: { $0.name.contains("iPhone") && $0.state == .booted }) else { print("No booted iPhone simulator found") return } // Take a screenshot print("Capturing screenshot of \(device.name)...") let screenshotData: Data = try await device.screenshot() // Save to file let outputURL = URL(fileURLWithPath: "/tmp/simulator_screenshot.jpg") try screenshotData.write(to: outputURL) print("Screenshot saved to \(outputURL.path)") print("Image size: \(screenshotData.count) bytes") } catch { print("Failed to capture screenshot: \(error)") } } ``` -------------------------------- ### Check Simulator Device State Source: https://context7.com/vmanot/simulatorkit/llms.txt Iterates through available devices and checks their operational state using the SimDeviceState enumeration. Useful for verifying device readiness before performing actions. ```swift import SimulatorKit Task { do { let devices = try await SimulatorDevice.all() for device in devices { switch device.state { case .creating: print("\(device.name): Being created") case .shutdown: print("\(device.name): Shutdown - ready to boot") case .booting: print("\(device.name): Currently booting...") case .booted: print("\(device.name): Running and ready") case .shuttingDown: print("\(device.name): Shutting down...") @unknown default: print("\(device.name): Unknown state") } } // Filter devices by state let bootedDevices = devices.filter { $0.state == .booted } let availableDevices = devices.filter { $0.state == .shutdown } print("\(bootedDevices.count) devices currently running") print("\(availableDevices.count) devices available to boot") } catch { print("Failed to get devices: \(error)") } } ``` -------------------------------- ### SimDeviceState - Device State Enumeration Source: https://context7.com/vmanot/simulatorkit/llms.txt Represents the current operational state of a simulator device. ```APIDOC ## SimDeviceState - Device State Enumeration ### Description Represents the current operational state of a simulator device. The state property on `SimulatorDevice` indicates whether the device is creating, shutdown, booting, booted, or shutting down. Use this to check device readiness before performing operations. ### Method N/A (This is an enumeration) ### Endpoint N/A ### Parameters None ### Request Example ```swift import SimulatorKit Task { do { let devices = try await SimulatorDevice.all() for device in devices { switch device.state { case .creating: print("\(device.name): Being created") case .shutdown: print("\(device.name): Shutdown - ready to boot") case .booting: print("\(device.name): Currently booting...") case .booted: print("\(device.name): Running and ready") case .shuttingDown: print("\(device.name): Shutting down...") @unknown default: print("\(device.name): Unknown state") } } // Filter devices by state let bootedDevices = devices.filter { $0.state == .booted } let availableDevices = devices.filter { $0.state == .shutdown } print("\(bootedDevices.count) devices currently running") print("\(availableDevices.count) devices available to boot") } catch { print("Failed to get devices: \(error)") } } ``` ### Response #### Success Response N/A (This is an enumeration used for checking device states) #### Response Example N/A ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.