Skip to content

Commit ab898dd

Browse files
hramosFacebook Github Bot
authored andcommitted
Set allowsBackgroundLocationUpdates as necessary when using location …
Summary: Set the flag 'allowsBackgroundLocationUpdates' to YES if the location background mode is enabled and the 'Always Allow Location' key is set in the App Plist. **motivation** We found that on iOS 9.x, the allowsBackgroundLocationUpdates flag must be set on location manager in order to receive updates when the app is in the background (seems to affect actual device only). **Test plan (required)** Example app using the forked branch [here](https://github.com/briancalvium/react-native-geolocation-pr-example). Run this example through XCode on a device with 9.x, observe that location update logs continue to appear when the app is in the background. Switch to react native 0.32.0, observe that location update logs stop once the app is in the background. Closes facebook#9717 Differential Revision: D4167685 Pulled By: hramos fbshipit-source-id: 5a62f8433bf8b553561a276fdaa544363298442a
1 parent 72b5170 commit ab898dd

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

Libraries/Geolocation/Geolocation.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ type GeoOptions = {
3939
*
4040
* ### iOS
4141
* You need to include the `NSLocationWhenInUseUsageDescription` key
42-
* in Info.plist to enable geolocation. Geolocation is enabled by default
43-
* when you create a project with `react-native init`.
42+
* in Info.plist to enable geolocation when using the app. Geolocation is
43+
* enabled by default when you create a project with `react-native init`.
44+
*
45+
* In order to enable geolocation in the background, you need to include the
46+
* 'NSLocationAlwaysUsageDescription' key in Info.plist and add location as
47+
* a background mode in the 'Capabilities' tab in Xcode.
4448
*
4549
* ### Android
4650
* To request access to location, you need to add the following line to your

Libraries/Geolocation/RCTLocationObserver.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ - (void)beginLocationUpdatesWithDesiredAccuracy:(CLLocationAccuracy)desiredAccur
144144
if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"] &&
145145
[_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
146146
[_locationManager requestAlwaysAuthorization];
147+
148+
// On iOS 9+ we also need to enable background updates
149+
NSArray* backgroundModes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIBackgroundModes"];
150+
if(backgroundModes && [backgroundModes containsObject:@"location"]) {
151+
if([_locationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
152+
[_locationManager setAllowsBackgroundLocationUpdates:YES];
153+
}
154+
}
147155
} else if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"] &&
148156
[_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
149157
[_locationManager requestWhenInUseAuthorization];

0 commit comments

Comments
 (0)