@brunowernimont
Let's connect together

Xcode launch arguments in Swift

March 7, 2021

Xcode Swift

Launch arguments are options that are send to your app on launch.

It’s helpful in case like:

  • to reset data
  • to disable onboarding
  • when testing

How to set launch argument within Xcode

Select your target and select “Edit scheme…”.

Edit scheme

Then add your arguments within the “Arguments Passed on Launch” dropdown.

Edit scheme

Checking argument presence

if CommandLine.arguments.contains("-reset") {
  ...
}

UI testing

func testRemovingContact() {
  let app = XCUIApplication()
  app.launchArguments = ["-reset", "-skipOndoarding"]
  app.launch()

  ...
}

Handling arguments in a struct

struct LaunchArgumentsHandler {
  func handle() {
    resetIfNeeded()
  }

  private func resetIfNeeded() {
    guard CommandLine.arguments.contains("-reset") else {
      return
  }

  ...
}