[11.0.3] requestLocation doesn't extend backgroundTimeRemaining

iOS: 11.0.3

Phone Model: MLLP2NF/A (iPhone SE)


I have an app that relies on regions to track the places a user has visited. When a region event has been triggered, the backgroundTimeRemaining property returns a high number (which is normal). After a few seconds, this value starts decreasing (the 180 sec limit started).

I sometimes need to process data for 4 minutes after a region event has been triggered. Since I only have at most 3 minutes, I call the requestLocation method when the backgroundTimeRemaining is low (~ 20 seconds) which should extend the remaining time.


This method works on my iphone SE with iOS 11.1 beta 3 (and on other phones too) but doesn't seem to work anymore on that particular device/OS.


Any idea? is this a bug?


PS: I've set the correct background mode, permissions and I also use the beginBackgroundTaskWithExpirationHandler method.

Replies

I've done some research and I think that this now works only while the app is open or shortly after the app is closed.


Suppose you call startUpdatingLocation to extend background time after a didExitRegion event. If you do it shortly after the app is closed, backgroundTimeRemaining increases from 180 to DBL_MAX and you'll see the following locationd log line:


default 11:27:11.413314 -0400 locationd {"msg":"#CLIUA Marking change", "clientKey":com.bar.foo, "reason":In-use halo-effect, "assertionLevel":3, "coming":1}


However, if you do it a few minutes after the app is closed, backgroundTimeRemaining continues to decrease and you'll see the following locationd log line instead:


default 11:35:39.423411 -0400 locationd #Warning Denying process assertion to com.bar.foo


After 180 seconds, your app is terminated.


Before iOS 11, it was possible to call startUpdatingLocation at any time to extend backgroundTimeRemaining. This is no longer the case on iOS 11.1 beta 4, and may have changed as earlier as iOS 11.0.3 or earlier.


I've also heard other reports of "stuck" or throttled background location updates on iOS 11. See this thread: https://forums.developer.apple.com/thread/86034


I've filed a bug report and will update this thread when I hear back.

In the project navigator, find the Info.plist file, right click on it, and select “Open As”, “Source Code”. Then, inside the top-level

<dict>
section, add:

<key>NSLocationWhenInUseUsageDescription</key>

<string>We'll show you cool things near you in the app.

</string> <!-- iOS 10 or earlier -->

<key>NSLocationAlwaysUsageDescription</key>

<string>We'll show you cool things near you in the app, and alert you via notifications if you don't have the app open.</string>

<!-- iOS 11 -->

<key>NSLocationAlwaysAndWhenInUsageDescription</key>

<string>We'll show you cool things near you in the app. With the "always" option, we can also alert you via notifications if you don't have the app open.</string>

Why the three keys and what do they mean?

  • NSLocationWhenInUseUsageDescription
    should describe how your app uses Location Services when it’s in use (or “in the foreground”).
  • NSLocationAlwaysUsageDescription
    should describe how your app uses Location Services both when in use, and in the background. This description is only for users of your app with iOS 10 or earlier.
  • NSLocationAlwaysAndWhenInUsageDescription
    should also describe how your app uses Location Services both when in use, and in the background. This description is only for users of your app with iOS 11. The users will be given an option to choose between “when in use” and “always” authorization.