Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 567
Can Poyrazoğlu Avatar asked Jul 21 '16 07:07

Can Poyrazoğlu


People also ask

How do you simulate a location in Simulation?

Click the location on the map you want to simulate, and then press the lowercase L key. A dialog will appear with the message, “Simulated Location Set”. After a short time, the simulated location you set in the Editor will appear as a pink dot in the Android Emulator. Click anywhere on the map, and press Shift+L.


1 Answers

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

like image 74
Yogesh Maheshwari Avatar answered Oct 04 '22 14:10

Yogesh Maheshwari