diff --git a/Libraries/Geolocation/Geolocation.js b/Libraries/Geolocation/Geolocation.js index 5f75e549fb..c8fd2890e8 100644 --- a/Libraries/Geolocation/Geolocation.js +++ b/Libraries/Geolocation/Geolocation.js @@ -71,6 +71,16 @@ type GeoOptions = { */ var Geolocation = { + /* + * Request suitable Location permission based on the key configured on pList. + * If NSLocationAlwaysUsageDescription is set, it will request Always authorization, + * although if NSLocationWhenInUseUsageDescription is set, it will request InUse + * authorization. + */ + requestAuthorization: function() { + RCTLocationObserver.requestAuthorization(); + }, + /* * Invokes the success callback once with the latest location info. Supported * options: timeout (ms), maximumAge (ms), enableHighAccuracy (bool) diff --git a/Libraries/Geolocation/RCTLocationObserver.m b/Libraries/Geolocation/RCTLocationObserver.m index e392db3203..d442ad7542 100644 --- a/Libraries/Geolocation/RCTLocationObserver.m +++ b/Libraries/Geolocation/RCTLocationObserver.m @@ -135,27 +135,7 @@ RCT_EXPORT_MODULE() - (void)beginLocationUpdatesWithDesiredAccuracy:(CLLocationAccuracy)desiredAccuracy distanceFilter:(CLLocationDistance)distanceFilter { - if (!_locationManager) { - _locationManager = [CLLocationManager new]; - _locationManager.delegate = self; - } - - // 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]; - } + [self requestAuthorization]; _locationManager.distanceFilter = distanceFilter; _locationManager.desiredAccuracy = desiredAccuracy; @@ -180,6 +160,31 @@ RCT_EXPORT_MODULE() #pragma mark - Public API +RCT_EXPORT_METHOD(requestAuthorization) +{ + if (!_locationManager) { + _locationManager = [CLLocationManager new]; + _locationManager.delegate = self; + } + + // 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]; + } +} + RCT_EXPORT_METHOD(startObserving:(RCTLocationOptions)options) { checkLocationConfig();