title: xcodebuild times out when starting simulator
body: <p>I am attempting to set up automated testing for my iOS app using TeamCity. Currently, my agent builds, but once building the last target and running codesign, it hangs and displays the following:</p>
  
  <pre><code>[19:29:34][CodeSign] CodeSign /var/root/Library/Developer/Xcode/DerivedData/MyProductiOS-fgkoogiimhlbckdlbckzmqfyaoff/Build/Products/Debug-iphonesimulator/MyProductiOSUITests-Runner.app/PlugIns/MyProductiOSUITests.xctest
  [19:29:34][CodeSign]     cd /Users/ericmiller/.tcagent/work/f9abef315a0137d4
  [19:29:34][CodeSign]     export CODESIGN_ALLOCATE=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/codesign_allocate
  [19:29:34][CodeSign]     export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/Users/ericmiller/.pyenv/shims:/Users/ericmiller/.rbenv/shims:/Users/ericmiller/.pyenv/shims:/Users/ericmiller/Scripts:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/opt/local/bin"
  [19:29:34][CodeSign] Signing Identity:     "-"
  [19:29:34][CodeSign]     /usr/bin/codesign --force --sign - --timestamp=none /var/root/Library/Developer/Xcode/DerivedData/MyProductiOS-fgkoogiimhlbckdlbckzmqfyaoff/Build/Products/Debug-iphonesimulator/MyProductiOSUITests-Runner.app/PlugIns/MyProductiOSUITests.xctest
  [19:29:34][CodeSign] /var/root/Library/Developer/Xcode/DerivedData/MyProductiOS-fgkoogiimhlbckdlbckzmqfyaoff/Build/Products/Debug-iphonesimulator/MyProductiOSUITests-Runner.app/PlugIns/MyProductiOSUITests.xctest: replacing existing signature
  [19:31:32][CodeSign] 2017-01-22 19:31:32.279 xcodebuild[28262:86807]  iOSSimulator: Timed out waiting 120 seconds for simulator to boot, current state is 1.
  [19:31:32][CodeSign] 2017-01-22 19:31:32.290 xcodebuild[28262:86805]  iOSSimulator: Timed out waiting 120 seconds for simulator to boot, current state is 1.
  [19:31:32][CodeSign] 2017-01-22 19:31:32.291 xcodebuild[28262:86797] Error Domain=IDETestOperationsObserverErrorDomain Code=3 "Timed out waiting 120 seconds for simulator to boot, current state is 1. If you believe this error represents a bug, please attach the log file at /var/root/Library/Developer/Xcode/DerivedData/MyProductiOS-fgkoogiimhlbckdlbckzmqfyaoff/Logs/Test/89F7C786-9E91-419F-98CA-F36385618DC2/Session-MyProductiOSTests-2017-01-22_192932-E95vcV.log" UserInfo={NSLocalizedDescription=Timed out waiting 120 seconds for simulator to boot, current state is 1. If you believe this error represents a bug, please attach the log file at /var/root/Library/Developer/Xcode/DerivedData/MyProductiOS-fgkoogiimhlbckdlbckzmqfyaoff/Logs/Test/89F7C786-9E91-419F-98CA-F36385618DC2/Session-MyProductiOSTests-2017-01-22_192932-E95vcV.log}
  [19:31:32][CodeSign] 2017-01-22 19:31:32.291 xcodebuild[28262:86797] Error Domain=IDETestOperationsObserverErrorDomain Code=3 "Timed out waiting 120 seconds for simulator to boot, current state is 1. If you believe this error represents a bug, please attach the log file at /var/root/Library/Developer/Xcode/DerivedData/MyProductiOS-fgkoogiimhlbckdlbckzmqfyaoff/Logs/Test/89F7C786-9E91-419F-98CA-F36385618DC2/Session-MyProductiOSUITests-2017-01-22_192932-mrX7wQ.log" UserInfo={NSLocalizedDescription=Timed out waiting 120 seconds for simulator to boot, current state is 1. If you believe this error represents a bug, please attach the log file at /var/root/Library/Developer/Xcode/DerivedData/MyProductiOS-fgkoogiimhlbckdlbckzmqfyaoff/Logs/Test/89F7C786-9E91-419F-98CA-F36385618DC2/Session-MyProductiOSUITests-2017-01-22_192932-mrX7wQ.log}
  </code></pre>
  
  <p>The simulator app is open in the dock. Clicking on the simulator does nothing, but if I right-click and select the window, I can see that the simulator is indeed running but is not displaying anything. <a href="https://i.stack.imgur.com/khpwI.png" rel="noreferrer">Example</a></p>
  
  <p>I'm looking for a workaround to test my code, without switching to a physical device instead of a simulator if possible. How can I achieve this goal, and what could be wrong with my configuration?</p>
  
  <p><a href="https://i.stack.imgur.com/XH2CZ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/XH2CZ.png" alt="Screenshot"></a></p>
  
  <p>EDIT:</p>
  
  <p>Sven Driemecker found the solution. Here's my configuration.
  Before running the xcodebuild, I run the following shell script to ensure a sanitary testing environment.</p>
  
  <pre><code>xcrun simctl shutdown %env.simulator_guid%
  killall Simulator
  killall com.apple.CoreSimulator.CoreSimulatorService
  xcrun simctl erase %env.simulator_guid%
  </code></pre>
  
  <p>Then, when running the xcodebuild, add the following option:</p>
  
  <pre><code>-destination "id=%env.simulator_guid%"
  </code></pre>
  
  <p>And finally, to clean everything up:</p>
  
  <pre><code>xcrun simctl shutdown %env.simulator_guid%
  killall Simulator
  killall com.apple.CoreSimulator.CoreSimulatorService
  </code></pre>
  
  <p><strong>Update:</strong> Here's a <a href="https://sosheskaz.github.io/2017/03/07/XCode-CI-Article.html" rel="noreferrer">blog post I wrote</a> about how to set up basic TeamCity CI for XCode/iOS.</p>