问题
I've just updated to XCode 4.2 and I see a cool feature that allows me to manually set the device location. Does anyone know of a way to accomplish the same thing programmatically? I'd like to set the location in some unit tests.
回答1:
The following AppleScript will let you set the location of the iOS Simulator. It should be possible to integrate this kind of script into a unit test script, or have your script generate the equivalent AppleEvents.
tell application "iOS Simulator"
activate
end tell
tell application "System Events"
tell process "iOS Simulator"
tell menu bar 1
tell menu bar item "Debug"
tell menu "Debug"
tell menu item "Location"
click
tell menu "Location"
click menu item "Custom Location…"
end tell
end tell
end tell
end tell
end tell
tell window 1
set value of text field 1 to "40.69"
set value of text field 2 to "-74.045"
click button "OK"
end tell
end tell
end tell
回答2:
You can use preprocessor conditional inclusion; check the TARGET_IPHONE_SIMULATOR
macro like this:
#if TARGET_IPHONE_SIMULATOR
float longitude = 39.1234;
// etc
#else
float longitude = myLocationManager.longitude
#endif
来源:https://stackoverflow.com/questions/8015336/programmatically-setting-iphone-simulator-location