Skip to content

Commit

Permalink
Issue #1403 - On iOS, add conditional compile of the request for 'alw…
Browse files Browse the repository at this point in the history
…ays' permission, to prevent users getting rejected by Apple for not including NSLocationAlwaysAndWhenInUseUsageDescription even if they never require background location updates.

The compile is conditional on the flag PERMISSION_LOCATION_ALWAYS being set in XCode under 'Preprocessor Macros'. If not set (the default) then the permission request for 'always' is not included in the binary, therefore not triggering a complaint by Apple.

The request for 'always' (background) location updates is rare, so it is reasonable to have this turned off by default, and require some effort by the developer to activate this.
  • Loading branch information
781flyingdutchman committed Jan 5, 2024
1 parent 6fb962c commit c933009
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 5 additions & 4 deletions geolocator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,17 @@ Starting from Android 10 you need to add the `ACCESS_BACKGROUND_LOCATION` permis
<details>
<summary>iOS</summary>

On iOS you'll need to add the following entries to your Info.plist file (located under ios/Runner) in order to access the device's location. Simply open your Info.plist file and add the following (make sure you update the description so it is meaningfull in the context of your App):
On iOS you'll need to add the following entry to your Info.plist file (located under ios/Runner) in order to access the device's location. Simply open your Info.plist file and add the following (make sure you update the description so it is meaningfull in the context of your App):

``` xml
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access to location when in the background.</string>
```

If you would like to receive updates when your App is in the background, you'll also need to add the Background Modes capability to your XCode project (Project > Signing and Capabilities > "+ Capability" button) and select Location Updates. Be careful with this, you will need to explain in detail to Apple why your App needs this when submitting your App to the AppStore. If Apple isn't satisfied with the explanation your App will be rejected.
If you would like to receive updates when your App is in the background, you'll also need to:
* Add the Background Modes capability to your XCode project (Project > Signing and Capabilities > "+ Capability" button) and select Location Updates. Be careful with this, you will need to explain in detail to Apple why your App needs this when submitting your App to the AppStore. If Apple isn't satisfied with the explanation your App will be rejected.
* Add an `NSLocationAlwaysAndWhenInUseUsageDescription` entry to your Info.plist (use `NSLocationAlwaysUsageDescription` if you're targeting iOS <11.0)
* Add a compiler flag: in XCode, click on Pods, choose the Target 'geolocator_apple', choose Build Settings, in the search box look for 'Preprocessor Macros' then add the 'PERMISSION_LOCATION_ALWAYS=1' flag (without the quotes)

When using the `requestTemporaryFullAccuracy({purposeKey: "YourPurposeKey"})` method, a dictionary should be added to the Info.plist file.
```xml
Expand Down
4 changes: 4 additions & 0 deletions geolocator_apple/ios/Classes/Handlers/PermissionHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ - (void) requestPermission:(PermissionConfirmation)confirmationHandler
if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"] != nil) {
[locationManager requestWhenInUseAuthorization];
}
#if PERMISSION_LOCATION_ALWAYS
else if ([self containsLocationAlwaysDescription]) {
[locationManager requestAlwaysAuthorization];
}
#endif
#endif
else {
if (self.errorHandler) {
Expand All @@ -89,6 +91,7 @@ - (void) requestPermission:(PermissionConfirmation)confirmationHandler
}
}

#if PERMISSION_LOCATION_ALWAYS
- (BOOL) containsLocationAlwaysDescription {
BOOL containsAlwaysDescription = NO;
if (@available(iOS 11.0, *)) {
Expand All @@ -99,6 +102,7 @@ - (BOOL) containsLocationAlwaysDescription {
? containsAlwaysDescription
: [[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"] != nil;
}
#endif

- (void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusNotDetermined) {
Expand Down

0 comments on commit c933009

Please sign in to comment.