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!

Hans
  • 287
  • 1
  • 5
  • 18

5 Answers5

17

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;
}
Kartikey
  • 4,516
  • 4
  • 15
  • 40
  • 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](https://github.com/kirillzyusko/react-native-wifi-p2p#startdiscoveringpeers) and I need differents permissions such as `ACCESS_COARSE_LOCATION`, `WRITE_EXTERNAL_STORAGE`... – Seba99 Oct 23 '22 at 13:57
  • Adding these permissions in `AndroidManifest.xml` would do the job according to the docs in the package you mentioned above. – Kartikey Oct 24 '22 at 05:54
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("...");
    }
  };
Fotios Tsakiris
  • 1,310
  • 1
  • 18
  • 24
2

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

Example with Location:

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

Documentation

BloodyMonkey
  • 1,572
  • 1
  • 9
  • 15
  • 1
    thx it works i upvoted the other because is was explained more detailed but your answer is correct! – Hans Aug 05 '21 at 15:03
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 :)

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" });
};
Sayan Dey
  • 173
  • 1
  • 8
  • 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](/help/how-to-answer). – Community Dec 08 '21 at 20:28