- Home /
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!
Answer by FatIgor · Nov 13, 2016 at 01:19 PM
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.
How do you check, was permission denied or granted? I need this for the microphone on the iPhone.
Answer by drewjosh · Nov 24, 2020 at 05:00 PM
For iOS I used the methods mentioned in the Unity docs here: https://docs.unity3d.com/ScriptReference/Application.RequestUserAuthorization.html
For Android also the methods from the docs: https://docs.unity3d.com/Manual/android-RequestingPermissions.html
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);
}
}
Answer by dansopanso · May 22, 2019 at 10:25 AM
Here is the Android Solution:
https://docs.unity3d.com/Manual/android-RequestingPermissions.html
Your answer
Follow this Question
Related Questions
how to request location , camera permission during run time ? 0 Answers
Check permissions on Android 2 Answers
How to make camera position relative to a specific target. 1 Answer
Rendering a level in a gallery style view? 0 Answers
Permission.RequestUserPermission(Permission.Camera) not working on Android 0 Answers