React Native expo-permission deprecated what to use now?
Asked Answered
E

5

11

I am using Permissions from the expo-permission Library to get the location coords of the user:

import * as Location from "expo-location";
import * as Permissions from 'expo-permissions';

const granted = await Permissions.askAsync(Permissions.LOCATION);

The above works but keeps giving the warning that expo-permissions is deprecated.

If I use:

import {Location, Permissions } from 'expo';

it says Cannot read property 'askAsync' of undefined.

Does someone know what i should use? I use sdk42

Thx!

Excommunicative answered 5/8, 2021 at 14:5 Comment(0)
M
20

As this blog by Brent Vatne says,

expo-permissions has been deprecated in favor of module-specific permissions methods You should migrate from using Permissions.askAsync and Permissions.getAsync to the permissions methods exported by modules that require the permissions.

For example: you should replace calls to Permissions.askAsync(Permissions.CAMERA) with Camera.requestPermissionsAsync()

There shouldn’t be two ways to do an identical thing in a single SDK, and so we picked our preferred approach and are consolidating around it.

So now, you will have to use Permissions from individual packages

For Location,

Firstly, install expo-location

expo install expo-location

Then you can use it like this

import * as Location from 'expo-location';

let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
  console.log('Permission to access location was denied');
  return;
}
Mitzimitzie answered 5/8, 2021 at 14:22 Comment(2)
And how can we add permissions that a package needs but doesn't provide any RequestXPermissionAsync() function ? I'm trying to use a wifi p2p lib and I need differents permissions such as ACCESS_COARSE_LOCATION, WRITE_EXTERNAL_STORAGE...Preset
Adding these permissions in AndroidManifest.xml would do the job according to the docs in the package you mentioned above.Mitzimitzie
H
6

If someone comes here and wants to get permissions for ImagePicker, then according to the docs you should do this:

import * as ImagePicker from "expo-image-picker";


  const getPermissionAsync = async () => {
    const { status } = await ImagePicker.requestMediaLibraryPermissionsAsync();
    if (status !== "granted") {
      alert("...");
    }
  };
Hidalgo answered 6/11, 2021 at 3:32 Comment(0)
A
2

Now with expo, each libs have their own permissions requests methods.

Example with Location:

let { status } = await Location.requestForegroundPermissionsAsync();

Documentation

Andriette answered 5/8, 2021 at 14:23 Comment(1)
thx it works i upvoted the other because is was explained more detailed but your answer is correct!Excommunicative
D
2

works:

import { Camera } from 'expo-camera';
import * as ImagePicker from "expo-image-picker";

const resultPermision = await Camera.requestCameraPermissionsAsync();
const resultPermisionCamera = resultPermision.status;

if (resultPermisionCamera === "denied") {
    toastRef.current.show("Gallery permissions are needed");
} else {
    const result = await ImagePicker.launchImageLibraryAsync({
        allowsEditing: true,
        aspect: [4, 3]
})

Now, expo-camera works great, but also I believe something is pending on the side of the app.json.

Do you still need to add these lines?:

"permissions": [
  "CAMERA",
  "WRITE_EXTERNAL_STORAGE",
  "CAMERA_ROLL"
]

question for everyone here :)

Desquamate answered 3/2, 2022 at 2:58 Comment(0)
C
1
import { Camera } from "expo-camera";

this.state = {
  hasCameraPermission: null,
  type: Camera.Constants.Type.back,
};
componentDidMount = async () => {
  const { status } = await Camera.requestCameraPermissionsAsync();
  this.setState({ hasCameraPermission: status === "granted" });
};
Curlew answered 8/12, 2021 at 11:38 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Situs

© 2022 - 2024 — McMap. All rights reserved.