I've been using react-native-permissions v1 to access the geolocation on Android and iOS. I have some RxJS code that wraps the library for easier usage. Previously all I needed to do was to add the native permission types to configuration files for Android/iOS and then call Permissions.request("location", { type: "whenInUse" }) and check the response.
It's however unclear to me that how the old way of requesting a single permission translates to asking for each permission separately in v2. The README has an example of calling multiple permissions, but that also means that each permission would need to be handled separately.
So, it would be nice if there would be a small migration guide that would show with examples how to migrate from v1 to v2. Asking for geolocation permission would be a good example to give as it works a bit different on Android and iOS.
It would also be nice if there would be some kind of way (new method?) to preserve the old behavior of only requesting one permission for both Android and iOS.
The changes has been made because the permissions differs way too much between the platforms (Android is more granular). An easy way to achieve the same behavior could be to create a file where you choose the permission per platform:
import { Platform } from "react-native";
import { check, request, PERMISSIONS } from "react-native-permissions";
const camera = Platform.select({
android: PERMISSIONS.ANDROID.CAMERA,
ios: PERMISSIONS.IOS.CAMERA,
});
const contacts = Platform.select({
android: PERMISSIONS.ANDROID.READ_CONTACTS,
ios: PERMISSIONS.IOS.CONTACTS,
});
export default {
checkCamera: () => check(camera),
requestCamera: () => request(camera),
checkContacts: () => check(contacts),
requestContacts: () => request(contacts),
};
Thanks @zoontek, that already helps a lot and that's one example to could be added to the README.
What about when using geolocation, where you can have multiple permissions on Android, e.g. PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION and
PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION? I guess that that in v1 those were taken care of explicitly.
Sorry for asking all these questions, but hopefully I can find a upgrade path for my project where I don't have to rewrite my whole geolocation handling when updating to the newest version.
@kristerkari In this case, you only need to request the "highest" permission (PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION), and PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION will be granted automatically.
Same if, for example, you request PERMISSIONS.ANDROID.WRITE_CONTACTS: PERMISSIONS.ANDROID.READ_CONTACTS will be granted. It's the classic Android behavior.
This project example app refresh all checks at each request so you can easily spot which permissions depends on another.
oh I did not know about that, that might be another thing that might be good to mention in the README now that the way of asking the permissions has changed.
Alright, so the migration changes for me were:
v1
import Permissions from "react-native-permissions";
Permissions.request("location", { type: "whenInUse" })
โ โ โ โ โ โ
"authorized"
v2
import { PERMISSIONS, request } from "react-native-permissions";
const permission = Platform.select({
android: PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION,
ios: PERMISSIONS.IOS.LOCATION_WHEN_IN_USE
});
request(permission)
โ โ โ โ โ โ
"granted"
@kristerkari That's right!
Most helpful comment
Alright, so the migration changes for me were:
v1
โ โ โ โ โ โ
v2
โ โ โ โ โ โ