Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit c0eaacb

Browse files
author
Dimitar Tachev
authored
Merge pull request #74 from NativeScript/ios-background-flags
[rebased] iOS: Adding options to use in long-running background tasks
2 parents 71fe85f + fbbe6de commit c0eaacb

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ geolocation.getCurrentLocation({ desiredAccuracy: Accuracy.high, maximumAge: 500
7474
| minimumUpdateTime | 5 secs | Minimum time interval between location updates, in milliseconds (ignored on iOS). |
7575
| maximumAge | - | How old locations to receive in ms. |
7676
| timeout | 5 minutes | How long to wait for a location in ms. |
77+
| iosAllowsBackgroundLocationUpdates | false | If enabled, UIBackgroundModes key in info.plist is required (check the hint below). Allow the application to receive location updates in background (ignored on Android) |
78+
| iosPausesLocationUpdatesAutomatically | true | Allow deactivation of the automatic pause of location updates (ignored on Android) |
79+
80+
> If iosAllowsBackgroundLocationUpdates is set to true, the following code is required in the info.plist file:
81+
>```
82+
><key>UIBackgroundModes</key>
83+
><array>
84+
> <string>location</string>
85+
></array>
86+
>```
7787
7888
### Methods
7989

src/geolocation.ios.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
successCallbackType,
1212
errorCallbackType
1313
} from "./location-monitor";
14+
import * as Platform from "platform";
1415

1516
const locationManagers = {};
1617
const locationListeners = {};
@@ -329,6 +330,14 @@ export class LocationMonitor {
329330
iosLocManager.distanceFilter = options ? options.updateDistance : minRangeUpdate;
330331
locationManagers[locListener.id] = iosLocManager;
331332
locationListeners[locListener.id] = locListener;
333+
if (parseInt(Platform.device.osVersion.split(".")[0]) >= 9) {
334+
iosLocManager.allowsBackgroundLocationUpdates =
335+
options && options.iosAllowsBackgroundLocationUpdates != null ?
336+
options.iosAllowsBackgroundLocationUpdates : false;
337+
}
338+
iosLocManager.pausesLocationUpdatesAutomatically =
339+
options && options.iosPausesLocationUpdatesAutomatically != null ?
340+
options.iosPausesLocationUpdatesAutomatically : true;
332341
return iosLocManager;
333342
}
334343
}

src/location-monitor.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@ export interface Options {
3333
* How long to wait for a location in ms.
3434
*/
3535
timeout?: number;
36+
37+
/**
38+
* A Boolean value which has to be set to true on iOS versions > 9.0 to allow the application to receive location updates in
39+
* background in combination with the UIBackgroundModes key 'location' in the Info.plist. An exception is thrown if the
40+
* property is enabled without the UIBackgroundModes key set to true. The value is ignored on Android.
41+
* @see {@link https://developer.apple.com/reference/corelocation/cllocationmanager/1620568-allowsbackgroundlocationupdates|allowsBackgroundLocationUpdates}
42+
*/
43+
iosAllowsBackgroundLocationUpdates?: boolean;
44+
45+
/**
46+
* A Boolean value which has to be set to false on iOS to deactivate the automatic pause of location updates. The location manager might pause
47+
* location updates for a period of time to improve battery life. This behavior may stop a long-running background task. Set this flag to false
48+
* to prevent this behavior. The value is ignored on Android.
49+
* @see {@link https://developer.apple.com/reference/corelocation/cllocationmanager/1620553-pauseslocationupdatesautomatical|pausesLocationUpdatesAutomatically}
50+
*/
51+
iosPausesLocationUpdatesAutomatically?: boolean;
3652
}
3753

3854
declare type successCallbackType = (location: Location) => void;

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-geolocation",
3-
"version": "4.0.0",
3+
"version": "4.1.0",
44
"description": "Provides API for getting and monitoring location for NativeScript app.",
55
"main": "geolocation",
66
"nativescript": {

0 commit comments

Comments
 (0)