Skip to content

Commit

Permalink
Enable background updates when NSLocationWhenInUseUsageDescription is…
Browse files Browse the repository at this point in the history
… set (#20911)

Summary:
Location updates are not working in background when user allows **When in use authorization**, but works when user allows **Always allow**.

In Geolocation/RCTLocationObserver.m, setAllowsBackgroundLocationUpdates are set only for **Always allow** case, but native iOS allows us to setAllowsBackgroundLocationUpdates even for **When in use authorization**.

Solution:
setAllowsBackgroundLocationUpdates is now set for both cases.

[iOS] [Fixed] RCTLocationObserver - Enable background location updates when NSLocationWhenInUseUsageDescription is set
Pull Request resolved: #20911

Differential Revision: D13891852

Pulled By: cpojer

fbshipit-source-id: 5aae8dc12a147e7bed688807f34466b0f5a69344
  • Loading branch information
Aditya Koukuntla authored and facebook-github-bot committed Feb 15, 2019
1 parent d27a4f9 commit 3ca3332
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions Libraries/Geolocation/RCTLocationObserver.m
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,17 @@ - (void)timeout:(NSTimer *)timer
_locationManager = [CLLocationManager new];
_locationManager.delegate = self;
}

// On iOS 9+ we also need to enable background updates
NSArray *backgroundModes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIBackgroundModes"];
if (backgroundModes && [backgroundModes containsObject:@"location"]) {
if ([_locationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
[_locationManager setAllowsBackgroundLocationUpdates:YES];
}
}
// Request location access permission
if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"] &&
[_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[_locationManager requestAlwaysAuthorization];

// On iOS 9+ we also need to enable background updates
NSArray *backgroundModes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIBackgroundModes"];
if (backgroundModes && [backgroundModes containsObject:@"location"]) {
if ([_locationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
[_locationManager setAllowsBackgroundLocationUpdates:YES];
}
}
} else if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"] &&
[_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[_locationManager requestWhenInUseAuthorization];
Expand Down

0 comments on commit 3ca3332

Please sign in to comment.