Check device access permissions [iOS and Android]

Hi everyone!

I’m working on a project where the device Camera, Mic and Gallery are used. The problem comes if the user doesn’t allow the app to access any of them. Is there any way to check device access permissions (both iOS and Android)?

Once checked, can I ask again for permissions (if previously denied)?

Thanks!

For iOS I used the methods mentioned in the Unity docs here: Unity - Scripting API: Application.RequestUserAuthorization

For Android also the methods from the docs: Unity - Manual: Request runtime permissions

I also needed to handle when permission was not granted. Here is my class (I need camera permission before I want to activate Vuforia Engine):

using UnityEngine;
using Vuforia;
#if UNITY_ANDROID
using UnityEngine.Android;
#elif UNITY_IOS
using UnityEngine.iOS;
#endif

public class CameraPermission : MonoBehaviour
{
    public GameObject permissionUIparent;
    public GameObject explanationUI;
    public GameObject noAccessUI;

    bool isVuforiaInitializing = false;
    VuforiaBehaviour vuforiaBehaviour;

    void Awake()
    {
        vuforiaBehaviour = GameObject.Find("AR Camera").GetComponent<VuforiaBehaviour>();
    }

    void Start()
    {
        permissionUIparent.SetActive(true);
        explanationUI.SetActive(true);
        noAccessUI.SetActive(false);
    }

    void OnGUI()
    {
#if PLATFORM_ANDROID
        if (!Permission.HasUserAuthorizedPermission(Permission.Camera))
        {
            // permission denied, no access should be visible, when activated when requested permission
            return;
        }
#elif UNITY_IOS
        if (!Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            // permission denied, no access should be visible, when activated when requested permission
            return;
        }
#endif
        // camera permission granted

        permissionUIparent.SetActive(false);

        if (!isVuforiaInitializing)
        {
            isVuforiaInitializing = true;
            ActivateVuforiaEngine();
        }
    }

    public void ActivateVuforiaEngine()
    {
        vuforiaBehaviour.enabled = true;
        VuforiaRuntime.Instance.InitVuforia();
    }

    public bool IsCameraPermissionGranted()
    {
#if UNITY_ANDROID
        return Permission.HasUserAuthorizedPermission(Permission.Camera);
#elif UNITY_IOS
        return Application.HasUserAuthorization(UserAuthorization.WebCam);
#endif
    }

    public void RequestCameraPermission()
    {
        noAccessUI.SetActive(true); // so it will be visible if user doesn't give permission
#if PLATFORM_ANDROID
        if (!Permission.HasUserAuthorizedPermission(Permission.Camera))
        {
            Permission.RequestUserPermission(Permission.Camera);
        }
#elif UNITY_IOS
        if (!Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            Application.RequestUserAuthorization(UserAuthorization.WebCam);
        }
#endif
    }

    public void ClickedNoAccessGranted()
    {
        noAccessUI.SetActive(true);
    }
}

@sajoga

An answer covering iOS.

https://forum.unity3d.com/threads/request-and-get-state-of-camera-permission.281858/

The final post in that thread tells you how to get the iOS permission state. Works, I’ve tried it.
It only deals specifically with the camera, but would be easy to modify for the other states you wanted.

I don’t know how to re-ask for permission, I just put up a dialog box telling them they will have to go to settings and give permission manually.

Not yet found a way of detecting on Android yet.

Here is the Android Solution: