question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Handle permission denied in geolocation service

See original GitHub issue

Environment

React Native Environment Info:
    System:
      OS: macOS High Sierra 10.13.6
      CPU: x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
      Memory: 29.59 MB / 16.00 GB
      Shell: 5.3 - /bin/zsh
    Binaries:
      Node: 10.6.0 - /usr/local/bin/node
      Yarn: 1.7.0 - /usr/local/bin/yarn
      npm: 6.1.0 - /usr/local/bin/npm
      Watchman: 4.7.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 11.3, macOS 10.13, tvOS 11.3, watchOS 4.3
      Android SDK:
        Build Tools: 23.0.1, 23.0.3, 25.0.0, 25.0.2, 25.0.3, 26.0.1, 27.0.3, 28.0.0
        API Levels: 23, 24, 25, 26, 27
    IDEs:
      Android Studio: 3.2 AI-181.5540.7.32.5056338
      Xcode: 9.3.1/9E501 - /usr/bin/xcodebuild
    npmPackages:
      @storybook/react-native: 3.4.11 => 3.4.11
      @types/react: 16.4.14 => 16.4.14
      @types/react-native: 0.55.22 => 0.55.22
      react: 16.5.2 => 16.5.2
      react-native: 0.57.2 => 0.57.2
    npmGlobalPackages:
      react-native-cli: 2.0.1
      react-native-git-upgrade: 0.2.7

Description

On Android, if the user denies location permissions, the getCurrentPosition function in Geolocation.js does not call the geo_error callback as expected.

As per the docs regarding permissions for android SDK 23+, access must be granted for certain permissions. In the case of the location permission, the RN Geolocation service handles this in the getCurrentPosition function (with some issues). These issues could be avoided by doing the android permission request yourself but it seems like this should either be fixed or removed from the geolocation service code to avoid confusion.

It appears the correct fix would involve moving the android permission check/request code into the android LocationModule.java file, as the rest of the error handling knowledge live within the java code and this is how it is done in iOS.

We have added a temporary fix for this internally by patching getCurrentPosition so it calls the geo_error callback if permission is denied (see below) but as mentioned, this does not seem to be the proper place for this code.

Figured I would open the issue in case anyone else is running into a similar issue, and with the hopes that this will eventually get properly fixed.

export const getCurrentPosition = async function(
  geo_success: Function,
  geo_error?: Function,
  geo_options?: GeoOptions
) {
  invariant(typeof geo_success === 'function', 'Must provide a valid geo_success callback.')
  let hasPermission = true
  // Supports Android's new permission model. For Android older devices,
  // it's always on.
  if (Platform.OS === 'android' && Platform.Version >= 23) {
    hasPermission = await PermissionsAndroid.check(
      PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
    )
    if (!hasPermission) {
      const status = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
      )
      hasPermission = status === PermissionsAndroid.RESULTS.GRANTED
      
      // the patch:
      if (!hasPermission) {
        geo_error({ code: PERMISSION_DENIED_ERROR_CODE })
      }
    }
  }
  if (hasPermission) {
    RCTLocationObserver.getCurrentPosition(geo_options || {}, geo_success, geo_error || logError)
  }
}

Reproducible Demo

Create a react native app and request location via the Geolocation servie. If location is denied on android, the geo_error callback will not be triggered as it is on iOS.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
Jyrno42commented, Dec 26, 2018

Working on this

1reaction
Jyrno42commented, Mar 30, 2019

This issue can probably be closed once GeoLocation has been removed from core RN. The fix for this bug has been filed in the new repository as: https://github.com/react-native-community/react-native-geolocation/pull/1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Permission denied - geolocation in React Native
In Simulator navigator, Choose Debug, at bottom choose Location, next choose Apple, then CMD+R to reload and it worked. enter image description here....
Read more >
Using the Permissions API - MDN Web Docs
function if permission is denied (which makes the "Enable Geolocation" button appear). "denied"
Read more >
Request location permissions - Android Developers
... Access the Wearable Data Layer · Transfer assets · Send and receive messages · Handle data layer events · Sync data items...
Read more >
Geolocation - React Native
You need to include the NSLocationWhenInUseUsageDescription key in Info.plist to enable geolocation when using the app. Geolocation is enabled ...
Read more >
Handling Background Location Permissions in React Native
Location-based applications are widely used nowadays in mobile apps. You may want to get a user's location to show services around, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found