Ask for multiple permissions


#1

How can you ask for multiple permissions? If, for example, I want to ask for Location and notifications permissions, is this good?

const { status } = await Permissions.askAsync(
        Permissions.NOTIFICATIONS,
        Permissions.LOCATION
      );

#2

Hey @jamesattard,

You’ll need to separate them into separate methods so you can determine the status of each.

Cheers,

Adam


#3

@jamesattard, Try this functions:

import Expo from 'expo'

export async function askAsyncMultiple(array){
    array = array instanceof Array ? array : arguments
    const result = []
    for(let i in array){
        let ask
        try {
            ask = await Expo.Permissions.askAsync(array[i])
        } catch(e) {
            ask = {status: 'error', error:e}
        }
        ask.type = array[i]
        result.push(ask)
    }
    return result
}
Expo.Permissions.askAsyncMultiple = askAsyncMultiple

Use like this

const perms = await Permissions.askAsync(
    Permissions.NOTIFICATIONS,
    Permissions.LOCATION
);
// perms[0].status
// perms[1].status

Or use like this

const perms = await Permissions.askAsync([
    Permissions.NOTIFICATIONS,
    Permissions.LOCATION
]);
// perms[0].status
// perms[1].status

#4

So I have followed the suggestions of asking for permissions separately as per the code snippet below, however I am only getting permission for one of the permissions. To be precise, I am only being asked to grant the location permission, while the notifications request is being completely ignored.

  registerLocationAndNotifications = async () => {
    let status;
    status = await Permissions.getAsync(Permissions.NOTIFICATIONS);
    let statusNotifications = status.status;
    console.log("Notifications Permissions: ", statusNotifications);

    status = await Permissions.getAsync(Permissions.LOCATION);
    let statusLocation = status.status;
    console.log("Location Permissions: ", statusLocation);

    if (statusNotifications !== "granted") {
      console.log("Requesting Notification Permissions");
      status = await Permissions.askAsync(Permissions.NOTIFICATIONS);
      statusNotifications = status.status;
    }

    if (statusLocation !== "granted") {
      console.log("Requesting Location Permissions");
      status = await Permissions.askAsync(Permissions.LOCATION);
      statusLocation = status.status;
    }

    if (statusNotifications !== "granted" || statusLocation !== "granted") {
      console.log("Permissions not granted");
      return;
    }

    console.log("Permissions Granted!");

I also created a new application and testing on the iOS simulator to make sure I start from ‘scratch’ (or am I really?). I have also repeated this with new applications, and still same issue. Refer to screenshots:


#5

Hi @jamesattard,
on Expo SDK 29, we have a bug:


closed #6

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.