Handle permission denied in geolocation service
See original GitHub issue- Review the documentation: https://facebook.github.io/react-native
- Search for existing issues: https://github.com/facebook/react-native/issues
- Use the latest React Native release: https://github.com/facebook/react-native/releases
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:
- Created 5 years ago
- Reactions:5
- Comments:5 (2 by maintainers)
Top GitHub Comments
Working on this
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