10

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.

Oliver
  • 23,072
  • 33
  • 138
  • 230
JonnyBoy
  • 1,555
  • 14
  • 24
  • http://stackoverflow.com/questions/214416/set-the-location-in-iphone-simulator – Matt Ball Nov 04 '11 at 20:26
  • 1
    I don't think that helps. What it's saying is to override the callback from CLLocationManager. But the problem I'm seeing is that in my unit tests, the callback isn't coming at all. I was hoping there was something like: [NSApplication setLatitude:lat longitude:lng] – JonnyBoy Nov 04 '11 at 23:54
  • so @JonnyBoy did you figure this out? i would love to use this as well! – abbood Oct 05 '13 at 13:39
  • @abbood no real solution that I'm happy with... – JonnyBoy Nov 26 '13 at 21:39

2 Answers2

10

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
Tom Gilder
  • 1,079
  • 1
  • 13
  • 21
Mike Akers
  • 12,039
  • 14
  • 58
  • 71
  • Sounds good. I'm not working with ios anymore, but I'll give this one to you because it looks like it does what I was asking for. Maybe someone else can verify? – JonnyBoy Jun 24 '14 at 16:41
  • `iOS Simulator` should be changed to `Simulator`, `Debug` to `Features`. And activating Simulator takes 2 minutes for me, but don't know why. – matebende Apr 06 '22 at 14:42
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
chown
  • 51,908
  • 16
  • 134
  • 170
  • 2
    I guess I wasn't clear enough. I'm looking for a way for a CLLocationManager instance to send a http://developer.apple.com/library/ios/DOCUMENTATION/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/CLLocationManagerDelegate/CLLocationManagerDelegate.html#//apple_ref/occ/intfm/CLLocationManagerDelegate/locationManager:didUpdateToLocation:fromLocation: as it would by clicking Debug->Location->Custom Location on the iphone simulator. This looks like all it does is set a float. – JonnyBoy Nov 04 '11 at 23:48