Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native expo-permission deprecated what to use now?

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!

like image 855
Hans Avatar asked Aug 05 '21 14:08

Hans


Video Answer


5 Answers

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;
}
like image 96
Kartikey Avatar answered Oct 19 '22 18:10

Kartikey


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("...");
    }
  };
like image 5
Fotios Tsakiris Avatar answered Oct 19 '22 19:10

Fotios Tsakiris


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

Example with Location:

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

Documentation

like image 2
BloodyMonkey Avatar answered Oct 19 '22 17:10

BloodyMonkey


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 :)

like image 2
nicoaguilar Avatar answered Oct 19 '22 18:10

nicoaguilar


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" });
};
like image 1
Sayan Dey Avatar answered Oct 19 '22 17:10

Sayan Dey