UIAutomation : Change location when the app is in background
Asked Answered
P

4

7

I am working on UIAutomation. I need to change the location when the app is in background. I have tried following line of code:

var target = UIATarget.localTarget();
UIATarget.localTarget().deactivateAppForDuration(30);
target.setLocation({longitude:77.50, latitude:12.50});

but target.setLocation is not getting called until the app comes to foreground.

Could any one please tell me how I can change the location when the app is in background.

Programme answered 12/3, 2014 at 7:37 Comment(5)
Did you try adding location to UIBackgroundModes key in info.plist file .. ?Airglow
@Airglow I didn't add but even if I try to log using UIALogger, that line will be executed after the app comes to foreground.Programme
@Airglow I tried after adding location to UIBackgroundModes but still no help.Programme
It's doubtful this is possible; only select specific services are ever available in the background (using UIBackgroundModes), and the app's entire UI layer is certainly not one of them. This is why you're seeing the call only when the app comes to foreground. Are you doing this for testing purposes? Why is it important?Educator
@itsthejb It's important for me for testing background location tracking, for simulating a route and speed.Seeing
I
5

Unfortunately UIAutomation can only automate the application it's attached to. When the application is not "active", it has nothing to talk to. Using lockForDuration, deactivateAppForDuration , etc blocks while it relinquishes control to the system, making your app inactive. While the application is in that state UIAutomation can't send it any more messages. I would recommend filing a radar asking for the ability to set the location while the application is inactive.

When I ran into a similar problem in the past I had to use AppleScript to change the location of the simulator to get the effect I needed.

You have since updated your comments indicating you are testing a navigation application. Well! That's a little different. If you do not ACTUALLY require UIAutomation and were only trying to use it as a way to set the location:

Xcode does allow you to simulate a route using GPX files, and this CAN be used with Instruments.

  1. Find an appropriate GPX file, preferably one that is a track.
  2. Go to the scheme for your project. In the Run action, go to the "Options" tab. Check "Allow Location Simulation" and use the drop down list to add your GPX.
  3. Run your application from Xcode.
  4. ...
  5. Profit!
Inutile answered 9/4, 2014 at 18:44 Comment(7)
Can you share some of your AppleScript to do that? Also, were you able to simulate speed with that?Seeing
I didn't try simulating movement, and the AppleScript is a hack that uses System Events (iOS Simulator itself does not have a scripting dictionary). For what you are trying to do, I would actually look at modifying the application to read a GPX file for a route, and send those locations to your core location delegate.Inutile
I was trying to use Instruments with UIAutomation, so that I could set the lat/lng, heading, velocity, etc.Seeing
If you're just trying to set those - there are other ways that are more appropriate for what you're trying to do. See my updated answer.Inutile
can you simulate velocity with a GPX file though?Seeing
Yes. A GPX track can/should include timestamps. So each item in it is a location with a timestamp. Xcode does interpret these correctly to "play back" at the correct velocity. I regularly use data captured by a Garmin Forerunner with Xcode.Inutile
Thanks for your help -- can you please send me a sample GPX if you have one?Seeing
F
1

I think you can use update part in background as service. Though i am newcomer in this world. You can try this. Thanks

Floribunda answered 26/3, 2014 at 2:23 Comment(1)
Can you elaborate on how exactly SO should do this? That answer doesn't help very much (i.e. at all).Gorget
H
1

if you want your app to execute methods in background, you have to enable "background mode" in your capabilities tab of your app. you have to choose a mode, for example "location updates". but be careful: apple has restrictions of what kind of apps are allowed to run in background. the "location updates" mode are only allowed for apps like navigation apps. see the app store guidelines for details.

if you dont set the "background mode" to yes, your app has 5 seconds to finish executing tasks after turning to background and then all tasks will be stopped and never called until your comes to foreground again.

Henze answered 9/4, 2014 at 9:44 Comment(2)
This is indeed for a navigation app that needs to run in the background. In order to properly test the app without having to actually go out into the real world, I want to be able to simulate changes in location in the background, but it seems UIAutomation won't let us do that unless the app is in the foreground.Seeing
Contact developer technical support and explain what are trying to do. They may have a solution for you that will work.Inutile
N
1

Refer the http://code.tutsplus.com/tutorials/ios-7-sdk-working-with-background-fetch--mobile-20520 for the Background fetch. I hope this helps you.

Nimiety answered 25/3, 2015 at 12:49 Comment(1)
A link to a potential solution is always welcome, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. Take into account that being barely more than a link to an external site is a possible reason as to Why and how are some answers deleted?.Sarracenia

© 2022 - 2024 — McMap. All rights reserved.