Simulate iPhone (real device, NOT simulator) location over USB programmatically
Asked Answered
C

2

12

I can simulate location through Xcode by preparing a GPX file with some coordinates, adding it to my project, running an app, and selecting the location when the app is running. Then, my whole iPhone's location is changed to that location (not just for that app).

In other words, it is possible to control iPhone's location over a Mac when it's connected through USB using Xcode.

I am wondering is there a way to automate this behavior? For example, I'll programmatically send the command to my iPhone to change it's location without manually creating a GPX file, adding it to a project, running a dummy app, and selecting a single GPX from the UI each time. Is there a way, maybe using command-line tools?

Crore answered 21/7, 2016 at 7:32 Comment(9)
Are you aware that you can set a default simulated location in the scheme manager, including with a custom GPX file?Deepdyed
@Deepdyed did you read the question? of course I know. I need a fast and automated way to do this, that's the whole question :)Cosma
I figured but just checking :) It seemed to me like it was possible you were setting the simulated location from the debug pane after launching the application. I do not know of a way do this from the command-line but I am curious to follow this thread.Deepdyed
@Deepdyed there should be a way. I mean, Xcode can talk to iPhone over USB and set it's location via it's GUI. so technically it is possible. the sad part is that I couldn't find anything related to this functionality. it may also be possible using UIAutomation or regular OS X automation, I don't know. but I'm sure that there should be a way.Cosma
yeah, my instinct tells me that it should be possible unless for security/tampering reasons it's something that Apple keeps tight control over regarding the devices. looking forward to learning from this thread, thoughDeepdyed
Is this answer of any help? https://mcmap.net/q/1012599/-set-location-simulator-city-gpx-during-runtimeHousum
And this https://mcmap.net/q/1012600/-automating-xcode-39-debug-gt-simulate-location-command.Housum
SHN thanks for sharing my answer. Swizzling CLLocation you can change location, but there is no way to set location from MAC. To set different location, you need a jailbroken device. Use fakelocation, locateme etc but only available in cydia.Overtrick
@Housum first one doesn't work (as I need to simulate device's location, not an app's) but the second one looks promising though it has a manual 'click event sending' mechanism to send the updated location to the device. I could actually get until there but something to automate that part would fit what I need more.Cosma
S
2

You can use Instruments to simulate location updates:

Via Automation It allows to write script using javascript, which will set Location and delay next location update.

Actually, simple setting location will look like:

var target = UIATarget.localTarget();
var location = {
    latitude: 59.335435,
    longitude: 18.017269
};
var locationOptions = {
    speed: 2.78,
    altitude: 200,
    horizontalAccuracy: 10,
    verticalAccuracy: 15
};
target.setLocationWithOptions(location, locationOptions);

To be able to run this script, you need to “Profile” your application from Xcode. For this go to Menu Product → Profile and your application will start alongside with Instruments. In Instruments select “Automation” and then create new script with body as above.

Your application should be automatically selected as Target in top left of Automation tool.

Just paste script to the Script area and press Run button. If app was not started, it will be started. Then script will run and you should see that location was set within your app.

You can just create array of several locations and locations options and then go in the loop through them. To make some delay, you can just use delay command.

var target = UIATarget.localTarget();

var locationOptions = {speed:2.78, altitude: 200, horizontalAccuracy:10, verticalAccuracy:15};
var locations = [
  {latitude: 59.335435, longitude: 18.017269},
  {latitude: 59.33618, longitude: 18.018288},
  {latitude: 59.337192, longitude: 18.01643},
  ...
  {latitude: 59.335769, longitude: 18.025336} 
];

for (var i = 0; i < locations.length; i++) {
    target.setLocationWithOptions(locations[i], locationOptions);
    target.delay(10);
}

Ref: http://sergiinezdolii.blogspot.com/2015/02/ios-simulate-frequent-gps-location.html

Shope answered 30/12, 2016 at 18:24 Comment(0)
E
0

You can setup a predefined route using the following site; GPX Generator. This creates a GPX file with a route at fast walking / running speed. A more automated method seems impossible according to my own research.

Eskimo answered 2/8, 2016 at 9:28 Comment(1)
Yes, I know it, but unfortunately it is not what I need as I need something programmable on the fly continuously.Cosma

© 2022 - 2024 — McMap. All rights reserved.