Skip to content

Commit 6b7f10d

Browse files
Adam Comellafacebook-github-bot
authored andcommitted
iOS: Geolocation: Allow skipping of permission prompts
Summary: This change enables developers to tell the Geolocation module to skip its permissions requests so the app can manage the permissions requests on its own. React Native Android's Geolocation module already requires apps to request permissions on their own. Currently, the Geolocation module automatically makes permissions requests for you based on what you've specified in your property list file. However, the property list file doesn't always represent what permissions the app wants right now. For example, suppose an app sometimes wants location when "in use" and sometimes wants location "always" depending on what app features the user has engaged with. The Geolocation API will request the "always" location permission even if that's not what the app wants for the current scenario. This change enables developers to tell the Geolocation module to skip permission requests so that the app can explicitly ask for the appropriate permissions. By default, Geolocation will still ask for permissions so this is not a breaking change. This change adds a method to Geolocation that is not supported by the web spec for geolocation (`setRNConfiguration`). This method includes `RN` in the name to minimize the chances that the web spec will introduce an API with the same name. **Test plan (required)** Verified that Geolocation requests permissions by default and when `skipPermissionRequests` is `false`. Verified the permission requests are skipped when `skipPermissionRequests` is `true`. Also, my team is using this change in our app. Adam Comella Microsoft Corp. Closes facebook#15096 Differential Revision: D5725563 Pulled By: javache fbshipit-source-id: fd23fedb7e57408fa0f0d0568e0f1ef2aea1cdd4
1 parent 46f0a3b commit 6b7f10d

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

Libraries/Geolocation/Geolocation.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ const PermissionsAndroid = require('PermissionsAndroid');
2626
var subscriptions = [];
2727
var updatesEnabled = false;
2828

29+
type GeoConfiguration = {
30+
skipPermissionRequests: bool;
31+
}
32+
2933
type GeoOptions = {
3034
timeout?: number,
3135
maximumAge?: number,
@@ -75,6 +79,25 @@ type GeoOptions = {
7579
*/
7680
var Geolocation = {
7781

82+
/*
83+
* Sets configuration options that will be used in all location requests.
84+
*
85+
* ### Options
86+
*
87+
* #### iOS
88+
*
89+
* - `skipPermissionRequests` - defaults to `false`, if `true` you must request permissions
90+
* before using Geolocation APIs.
91+
*
92+
*/
93+
setRNConfiguration: function(
94+
config: GeoConfiguration
95+
) {
96+
if (RCTLocationObserver.setConfiguration) {
97+
RCTLocationObserver.setConfiguration(config);
98+
}
99+
},
100+
78101
/*
79102
* Request suitable Location permission based on the key configured on pList.
80103
* If NSLocationAlwaysUsageDescription is set, it will request Always authorization,

Libraries/Geolocation/RCTLocationObserver.m

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ typedef NS_ENUM(NSInteger, RCTPositionErrorCode) {
2727

2828
#define RCT_DEFAULT_LOCATION_ACCURACY kCLLocationAccuracyHundredMeters
2929

30+
typedef struct {
31+
BOOL skipPermissionRequests;
32+
} RCTLocationConfiguration;
33+
3034
typedef struct {
3135
double timeout;
3236
double maximumAge;
@@ -37,6 +41,15 @@ typedef NS_ENUM(NSInteger, RCTPositionErrorCode) {
3741

3842
@implementation RCTConvert (RCTLocationOptions)
3943

44+
+ (RCTLocationConfiguration)RCTLocationConfiguration:(id)json
45+
{
46+
NSDictionary<NSString *, id> *options = [RCTConvert NSDictionary:json];
47+
48+
return (RCTLocationConfiguration) {
49+
.skipPermissionRequests = [RCTConvert BOOL:options[@"skipPermissionRequests"]]
50+
};
51+
}
52+
4053
+ (RCTLocationOptions)RCTLocationOptions:(id)json
4154
{
4255
NSDictionary<NSString *, id> *options = [RCTConvert NSDictionary:json];
@@ -111,6 +124,7 @@ @implementation RCTLocationObserver
111124
NSMutableArray<RCTLocationRequest *> *_pendingRequests;
112125
BOOL _observingLocation;
113126
BOOL _usingSignificantChanges;
127+
RCTLocationConfiguration _locationConfiguration;
114128
RCTLocationOptions _observerOptions;
115129
}
116130

@@ -141,7 +155,9 @@ - (dispatch_queue_t)methodQueue
141155

142156
- (void)beginLocationUpdatesWithDesiredAccuracy:(CLLocationAccuracy)desiredAccuracy distanceFilter:(CLLocationDistance)distanceFilter useSignificantChanges:(BOOL)useSignificantChanges
143157
{
144-
[self requestAuthorization];
158+
if (!_locationConfiguration.skipPermissionRequests) {
159+
[self requestAuthorization];
160+
}
145161

146162
_locationManager.distanceFilter = distanceFilter;
147163
_locationManager.desiredAccuracy = desiredAccuracy;
@@ -172,6 +188,11 @@ - (void)timeout:(NSTimer *)timer
172188

173189
#pragma mark - Public API
174190

191+
RCT_EXPORT_METHOD(setConfiguration:(RCTLocationConfiguration)config)
192+
{
193+
_locationConfiguration = config;
194+
}
195+
175196
RCT_EXPORT_METHOD(requestAuthorization)
176197
{
177198
if (!_locationManager) {

0 commit comments

Comments
 (0)