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

[rebased] iOS: Adding options to use in long-running background tasks #74

Merged
merged 8 commits into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ geolocation.getCurrentLocation({ desiredAccuracy: Accuracy.high, maximumAge: 500
| minimumUpdateTime | 5 secs | Minimum time interval between location updates, in milliseconds (ignored on iOS). |
| maximumAge | - | How old locations to receive in ms. |
| timeout | 5 minutes | How long to wait for a location in ms. |
| 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) |
| iosPausesLocationUpdatesAutomatically | true | Allow deactivation of the automatic pause of location updates (ignored on Android) |

> If iosAllowsBackgroundLocationUpdates is set to true, the following code is required in the info.plist file:
>```
><key>UIBackgroundModes</key>
><array>
> <string>location</string>
></array>
>```

### Methods

Expand Down
9 changes: 9 additions & 0 deletions src/geolocation.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
successCallbackType,
errorCallbackType
} from "./location-monitor";
import * as Platform from "platform";

const locationManagers = {};
const locationListeners = {};
Expand Down Expand Up @@ -329,6 +330,14 @@ export class LocationMonitor {
iosLocManager.distanceFilter = options ? options.updateDistance : minRangeUpdate;
locationManagers[locListener.id] = iosLocManager;
locationListeners[locListener.id] = locListener;
if (parseInt(Platform.device.osVersion.split(".")[0]) >= 9) {
iosLocManager.allowsBackgroundLocationUpdates =
options && options.iosAllowsBackgroundLocationUpdates != null ?
options.iosAllowsBackgroundLocationUpdates : false;
}
iosLocManager.pausesLocationUpdatesAutomatically =
options && options.iosPausesLocationUpdatesAutomatically != null ?
options.iosPausesLocationUpdatesAutomatically : true;
return iosLocManager;
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/location-monitor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ export interface Options {
* How long to wait for a location in ms.
*/
timeout?: number;

/**
* A Boolean value which has to be set to true on iOS versions > 9.0 to allow the application to receive location updates in
* background in combination with the UIBackgroundModes key 'location' in the Info.plist. An exception is thrown if the
* property is enabled without the UIBackgroundModes key set to true. The value is ignored on Android.
* @see {@link https://developer.apple.com/reference/corelocation/cllocationmanager/1620568-allowsbackgroundlocationupdates|allowsBackgroundLocationUpdates}
*/
iosAllowsBackgroundLocationUpdates?: boolean;

/**
* 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
* 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
* to prevent this behavior. The value is ignored on Android.
* @see {@link https://developer.apple.com/reference/corelocation/cllocationmanager/1620553-pauseslocationupdatesautomatical|pausesLocationUpdatesAutomatically}
*/
iosPausesLocationUpdatesAutomatically?: boolean;
}

declare type successCallbackType = (location: Location) => void;
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript-geolocation",
"version": "4.0.0",
"version": "4.1.0",
"description": "Provides API for getting and monitoring location for NativeScript app.",
"main": "geolocation",
"nativescript": {
Expand Down