Exposes requestAuthorization method.

Reviewed By: javache

Differential Revision: D5129014

fbshipit-source-id: 1e0be6a8a00a2f38ca7b5b20d891148c5ba97467
This commit is contained in:
Marcus do Nascimento 2017-05-25 07:01:17 -07:00 коммит произвёл Facebook Github Bot
Родитель 7837bdbf7c
Коммит 3c64d95413
2 изменённых файлов: 36 добавлений и 21 удалений

Просмотреть файл

@ -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)

Просмотреть файл

@ -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();