Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requesting iPhone location whilst in background?

Simple question ... I have an application that records a users location at 30second intervals (using an NSTimer) it works perfectly until the application goes "inactive" and the NStimer stops. As a consequence I am looking for options to maintain my location interval (30secs) whilst still being able to record fairly accurate location data (within 100m accuracy).

  • Option_001, Brute Force: Let CLLocationManager, startUpdatingLocation run all the time using UIBackgroundModes = "location". Not recommended, drains battery. Regularity upon request, Accuracy approx. 10-65m. Might just be the only realistic option.

  • Option_002, SLC: I could use Significant Location Change but the frequency of location updates is pretty poor (not to mention accuracy). This is particularly true if the application is running in a rural or wilderness area with limited numbers of cell towers. Regularity unknown, Accuracy approx. 500m

  • Option_003, Hybrid: I could use Significant Location Change (SLC) in the background as an indicator of "significant" movement and then request an GPS location based on kCLLocationAccuracyBest. This would work but the SLC events are not going to arrive at anywhere near 30second intervals (particularly when walking). Regularity unknown, Accuracy approx. 10-50m.

  • Option_004, Something else? any ideas would be much appreciated.


NOTE: I thought I had this working because when you press [LOCK] on an iPhone (connected via USB) applicationWillResignActive is called but NSTimers do not stop. If you try the same with the iPhone un-connected (i.e. as the phone would be in normal use) the NSTimers stop almost immediately after applicationWillResignActive is called.

like image 418
fuzzygoat Avatar asked Apr 27 '11 16:04

fuzzygoat


People also ask

How do I force my iPhone to location?

Here's how: Go to Settings > Privacy, then select Location Services. Select an app, then turn Precise Location on or off.

Can you make your location say you're somewhere else on iPhone?

There isn't a “fake GPS location” setting built in to either iOS or Android, and neither do most apps let you spoof your location through a simple option. Setting up your phone to use fake GPS only affects your location.

Does Apple tell you when someone's looking at your location?

They don't get any notification or there isn't any log of lookup incidents made available to them. The location is sent from the friends device only when you request to see it.

Does Apple Maps run in the background?

As high-tech as your iPhone and its battery may be, a navigation app such as Google Maps and Apple's own native Map app typically runs in the background of your system. This hidden activity forces your smartphone to search for a GPS signal continuously, thus, pulling energy from your phone's battery.


2 Answers

First of all, don't use a timer to update the user location. Approach it from the other end: check, when a new location is received, the interval since the last "recording" and decide if you want to record the new location or not.

Also, this will get around your "inactive" state problem. Just enable background location services. Info.plist > Required background modes > App registers for location updates

Whilst in background, when a new location is received, your app will go in a "background active" state that will allow enough time to make an API call and push the new location.

In a sentence, you need to design this app to work well with the new background modes.

Note: this solution won't work for iOS3.x

like image 150
Paul Ardeleanu Avatar answered Oct 03 '22 08:10

Paul Ardeleanu


You can add the core location in the plist background activity.

plist > "Required background modes" > "App registers for location updates"

The location manager call back

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

Will work while your app goes to background.

like image 29
TamirTLV Avatar answered Oct 03 '22 07:10

TamirTLV