4

I'm running automated application unit tests under a C.I. environment for iPhone apps and everything works fine using a command line such as;

xcodebuild -scheme "Tests" -configuration Debug -sdk iphonesimulator5.0

That's great, but I now want to force the iPhone simulator to launch in Retina mode, rather than the default standard mode.

I know I can toggle this with the hardware menu option, however I'm running this on a headless integration server so don't have that option.

What I'd like to do is pass a switch somehow on the command line that tells the simulator to launch in Retina mode.

I optimistically tried appending SimulateDevice="iPhone (Retina)" to the xcodebuild command, but that seems not to work.

Is this possible? I can't seem to find a way at the moment.

If it's not possible, is there an alternative approach I can take?

Roger
  • 15,793
  • 4
  • 51
  • 73
  • Is your **xcodebuild** command actually launching the app in the simulator? Tell me how! – Andrew Wolfe Apr 09 '12 at 19:05
  • See https://gist.github.com/1365687 and http://lifeandcode.net/2011/12/automated-ios-jenkins-builds-with-application-tests-and-core-data/ and http://longweekendmobile.com/2011/04/17/xcode4-running-application-tests-from-the-command-line-in-ios/ for some of the references I followed – Roger Apr 10 '12 at 12:54

7 Answers7

5

Another approach looks like;

defaults write com.apple.iphonesimulator "SimulateDevice" '"iPhone (Retina)"'

However as with the applescript approach from sch, this doesn't seem to be quite working when used as part of the build phase. Still investigating ...

Roger
  • 15,793
  • 4
  • 51
  • 73
  • Neither seem to work as part of a run script, I'll try the restarting simulator bit though... but how do I do that from the command line? – Roger Mar 06 '12 at 13:34
  • Solved the restarting simulator with another script from here; http://stackoverflow.com/questions/5125243/how-can-i-reset-the-ios-simulator-from-the-command-line – Roger Mar 06 '12 at 13:50
3

You should use AppleScript to change the value of SimulateDevice in com.apple.iphonesimulator.plist.

Here is an example that does that after prompting the user to choose the desired device type. You can modify it to read the value from the command line or use "iPhone (Retina)" as a default value.

The following script changes the simulator device into a value from the command line:

on run argv

set selectedDevice to item 1 of argv as string

set thePListFolderPath to path to preferences folder from user domain as string
set thePListPath to thePListFolderPath & "com.apple.iphonesimulator.plist"
tell application "System Events"
    tell property list file thePListPath
        tell contents
            set value of property list item "SimulateDevice" to selectedDevice
        end tell
    end tell
end tell

end run

And you can execute it from the terminal using the command osascript:

osascript myScript.scpt "iPhone (Retina)"

Or

osascript myScript.scpt "iPhone"

Edit

You can modify that script to make it launch the Retina simulator by default:

set selectedDevice to "iPhone (Retina)"

set thePListFolderPath to path to preferences folder from user domain as string
set thePListPath to thePListFolderPath & "com.apple.iphonesimulator.plist"
tell application "System Events"
    tell property list file thePListPath
        tell contents
            set value of property list item "SimulateDevice" to selectedDevice
        end tell
    end tell
end tell

Finally note that changes to "SimulateDevice" take effect only when a new simulator is launched.

sch
  • 27,436
  • 3
  • 68
  • 83
  • Liking the approach ... on Lion this doesn't work though. It looks like arch -i386 osacript ... will do the trick though. Now I'm getting script error: Expected “end” or “end tell” but found unknown token. (-2741) – Roger Mar 06 '12 at 12:49
  • OK so I'm actually using the defaults write option as that was easier, but you absolutely set me on the right track, and I needed applescript for the restart, so I'm happy to accept this answer - many thanks for taking the time to expand on your answer. – Roger Mar 06 '12 at 13:51
2

For anyone scripting in Ruby, here is a method to set the simulated device in the simulator:

  def set_simulated_device(simulated_device)
    current_simulated_device = `defaults read com.apple.iphonesimulator "SimulateDevice"`.chomp

    if current_simulated_device != simulated_device
      simulator_pid = `ps -ax|awk '/[i]Phone Simulator.app\\/Contents\\/MacOS\\/iPhone Simulator/{print $1}'`.chomp
      Process.kill('INT', simulator_pid.to_i) unless simulator_pid.empty?
      `defaults write com.apple.iphonesimulator "SimulateDevice" '"#{simulated_device}"'`
    end
  end


List of Arguments that can be used

"iPhone Retina (3.5-inch)"
"iPhone Retina (4-inch)"
"iPhone Retina (4-inch 64-bit)"
"iPad"
"iPad Retina"
"iPad Retina (64-bit)"

Tejasvi Manmatha
  • 564
  • 7
  • 12
phatmann
  • 18,161
  • 7
  • 61
  • 51
0

You can do this from the iPhone Simulator binary

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone Simulator.app/Contents/MacOS/iPhone\ Simulator -SimulateDevice "iPhone Retina (4-inch)"

for other versions:

  • "iPhone Retina (3.5-inch)"
  • "iPhone Retina (4-inch)"
  • "iPhone Retina (4-inch 64-bit)"
  • "iPad"
  • "iPad Retina"
  • "iPad Retina (64-bit)"
Dan Cuellar
  • 269
  • 2
  • 3
0

not telling any answers are wrong.. but this worked for ios 7

-- START:choose.sim.device
on run argv
    --set simType to item 1 of argv
    tell application "iPhone Simulator"
        activate
    end tell
    set simType to "Ipad Retina (64-bit)"
    tell application "System Events"
        tell process "iOS Simulator"
            tell menu bar 1
                -- Hardware menu bar item
                tell menu bar item 5
                    -- Hardware menu
                    tell menu 1
                        -- Device menu item
                        tell menu item 1
                            -- Device sub menu
                            tell menu 1
                                click menu item simType
                            end tell
                        end tell
                    end tell
                end tell
            end tell
        end tell
    end tell
    -- END:choose.sim.device

    -- Need to show the simulator again after changing device,
    -- or else the simulator be hidden when launched by instruments
    -- for some odd reason.
    tell application "System Events"
        set visible of process "iPhone Simulator" to true
    end tell

    -- START:choose.sim.device
end run
-- END:choose.sim.device

Note: its not "iPad" its "Ipad"

Prajwal Udupa
  • 860
  • 5
  • 28
0

Pro-tip, create an alias.

alias ios="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone\ Simulator.app/Contents/MacOS/iPhone\ Simulator  -SimulateDevice”

Example:

ios "iPhone Retina (3.5-inch)"
ios "iPhone Retina (4-inch)"
ios "iPhone Retina (4-inch 64-bit)"
ios "iPad"
ios "iPad Retina"
ios "iPad Retina (64-bit)"
Daniel Smith
  • 1,044
  • 10
  • 16
0

You can specify the device with the -destination flag:

xcodebuild -scheme "Tests"      \
           -configuration Debug \
           -destination "platform=iOS Simulator,name=iPhone 6"
dstnbrkr
  • 4,305
  • 22
  • 23