Skip to content

iOS: Geolocation: Allow skipping of permission prompts #15096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
23 changes: 23 additions & 0 deletions Libraries/Geolocation/Geolocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const PermissionsAndroid = require('PermissionsAndroid');
var subscriptions = [];
var updatesEnabled = false;

type GeoConfiguration = {
skipPermissionRequests: bool;
}

type GeoOptions = {
timeout?: number,
maximumAge?: number,
Expand Down Expand Up @@ -75,6 +79,25 @@ type GeoOptions = {
*/
var Geolocation = {

/*
* Sets configuration options that will be used in all location requests.
*
* ### Options
*
* #### iOS
*
* - `skipPermissionRequests` - defaults to `false`, if `true` you must request permissions
* before using Geolocation APIs.
*
*/
setRNConfiguration: function(
config: GeoConfiguration
) {
if (RCTLocationObserver.setConfiguration) {
RCTLocationObserver.setConfiguration(config);
}
},

/*
* Request suitable Location permission based on the key configured on pList.
* If NSLocationAlwaysUsageDescription is set, it will request Always authorization,
Expand Down
23 changes: 22 additions & 1 deletion Libraries/Geolocation/RCTLocationObserver.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ typedef NS_ENUM(NSInteger, RCTPositionErrorCode) {

#define RCT_DEFAULT_LOCATION_ACCURACY kCLLocationAccuracyHundredMeters

typedef struct {
BOOL skipPermissionRequests;
} RCTLocationConfiguration;

typedef struct {
double timeout;
double maximumAge;
Expand All @@ -37,6 +41,15 @@ typedef NS_ENUM(NSInteger, RCTPositionErrorCode) {

@implementation RCTConvert (RCTLocationOptions)

+ (RCTLocationConfiguration)RCTLocationConfiguration:(id)json
{
NSDictionary<NSString *, id> *options = [RCTConvert NSDictionary:json];

return (RCTLocationConfiguration) {
.skipPermissionRequests = [RCTConvert BOOL:options[@"skipPermissionRequests"]]
};
}

+ (RCTLocationOptions)RCTLocationOptions:(id)json
{
NSDictionary<NSString *, id> *options = [RCTConvert NSDictionary:json];
Expand Down Expand Up @@ -111,6 +124,7 @@ @implementation RCTLocationObserver
NSMutableArray<RCTLocationRequest *> *_pendingRequests;
BOOL _observingLocation;
BOOL _usingSignificantChanges;
RCTLocationConfiguration _locationConfiguration;
RCTLocationOptions _observerOptions;
}

Expand Down Expand Up @@ -141,7 +155,9 @@ - (dispatch_queue_t)methodQueue

- (void)beginLocationUpdatesWithDesiredAccuracy:(CLLocationAccuracy)desiredAccuracy distanceFilter:(CLLocationDistance)distanceFilter useSignificantChanges:(BOOL)useSignificantChanges
{
[self requestAuthorization];
if (!_locationConfiguration.skipPermissionRequests) {
[self requestAuthorization];
}

_locationManager.distanceFilter = distanceFilter;
_locationManager.desiredAccuracy = desiredAccuracy;
Expand Down Expand Up @@ -172,6 +188,11 @@ - (void)timeout:(NSTimer *)timer

#pragma mark - Public API

RCT_EXPORT_METHOD(setConfiguration:(RCTLocationConfiguration)config)
{
_locationConfiguration = config;
}

RCT_EXPORT_METHOD(requestAuthorization)
{
if (!_locationManager) {
Expand Down