Search Unity

RequestUserAuthorization as a coroutine - bugged?

Discussion in 'Editor & General Support' started by doggan, Jan 19, 2016.

  1. doggan

    doggan

    Joined:
    Aug 12, 2012
    Posts:
    23
    According to the Unity docs (link) requesting permission for microphone/camera/etc can be done as a coroutine with a yield. You should then be able to query whether or not the authorization was granted as such:

    Code (CSharp):
    1. IEnumerator Start() {
    2.   yield return Application.RequestUserAuthorization(UserAuthorization.Microphone);
    3.  
    4.   if (Application.HasUserAuthorization(UserAuthorization.Microphone)) {
    5.     Debug.Log("Got permission!");
    6.   } else {
    7.     Debug.Log("No permission!");
    8.   }
    9. }
    We have code like this in our app, but we are getting reports from users that it is entering the "No permission!" block of code even after they have selected "Allow" in the iOS permission dialogue popup. When they go to Settings and view the actual app permissions, though, the permission has been granted.

    Does it take a certain amount of time before permission is actually granted? I've tried waiting a few extra frames (yield return null) in between the RequestUserAuthorization and HasUserAuthorization check, but it is still happening. I would expect the result returned from HasUserAuthorization to be immediately valid.

    We're using Unity 5.1.4f1, and we've had reports of this happening on both iOS 8 and 9. It doesn't happen all the time, and it seems very difficult to reproduce.

    Is there a reason this is happening, or is it a bug?
     
    Last edited: Jan 19, 2016
  2. ywj7931

    ywj7931

    Joined:
    Jun 24, 2015
    Posts:
    41
    having same problem
     
  3. dseabolt_il

    dseabolt_il

    Joined:
    Jan 18, 2018
    Posts:
    6
    Did you guys ever find out the cause of this? I'm getting the same issue.
     
  4. dseabolt_il

    dseabolt_il

    Joined:
    Jan 18, 2018
    Posts:
    6
    For future readers, Application.HasUserAuthorization will return false for some requests on iOS even if the user gave permission. In my case I was asking for Microphone and then WebCam authorization. But iOS does track it as affirmative permission!

    My (crappy) workaround is just sit in an update loop and poll until it starts to work. It takes a couple of seconds in my case to catch, so a simple WaitForSeconds(0.25f) didn't work. Hopefully Unity fixes this someday.
     
    Archviz3d likes this.
  5. mbo42

    mbo42

    Joined:
    Aug 4, 2017
    Posts:
    1
    I'm very new to Unity, but did iOS previously. I noticed that since the permission alerts are system alerts, the app goes into background as soon as the alert appears. When the alert gets dismissed, the app becomes focused again.

    The going to background is probably the reason why the code returns false instantly. I tried this with a native plugin as well - and got the same result.

    My solution is to check for the result when the app becomes focused again (in the function OnApplicationFocus).

    You might also want to add a flag to check if permission has been asked before (iOS only shows permission alert once), and only check for result then.

    Works well on the iOS devices that I've tested.
     
    Last edited: May 19, 2019
  6. bkachmar

    bkachmar

    Joined:
    Mar 15, 2013
    Posts:
    43
    Thanks for the idea.
    In case someone will look for the implementation - here is what works well for me on iOS to request both Microphone and Camera access:

    Code (CSharp):
    1. public class PermissionRequesterBehaviour : MonoBehaviour
    2. {
    3.     private bool RequestingCameraPermissionNow = false;
    4.     private bool RequestingMicrophonePermissionNow = false;
    5.  
    6.     // Start is called before the first frame update
    7.     void Start()
    8.     {
    9.         StartCoroutine(RequestCameraPermissionCoroutine());
    10.     }
    11.  
    12.     private IEnumerator RequestCameraPermissionCoroutine()
    13.     {
    14.         Debug.Log("RequestCameraPermissionCoroutine");
    15.      
    16.         // Wait for a few frames for the app to process everything
    17.         yield return null;
    18.         yield return new WaitForEndOfFrame();
    19.         yield return null;
    20.         yield return new WaitForEndOfFrame();
    21.      
    22.         // Request camera permission and wait in OnApplicationFocus
    23.         RequestingCameraPermissionNow = true;
    24.         Application.RequestUserAuthorization(UserAuthorization.WebCam);
    25.     }
    26.  
    27.     private IEnumerator RequestMicrophonePermissionCoroutine()
    28.     {
    29.         Debug.Log("RequestMicrophonePermissionCoroutine");
    30.      
    31.         // Wait for a few frames for the app to process everything
    32.         yield return null;
    33.         yield return new WaitForEndOfFrame();
    34.         yield return null;
    35.         yield return new WaitForEndOfFrame();
    36.      
    37.         // Request microphone permission and wait in OnApplicationFocus
    38.         RequestingMicrophonePermissionNow = true;
    39.         Application.RequestUserAuthorization(UserAuthorization.Microphone);
    40.     }
    41.  
    42.     private IEnumerator LoadNextSceneCoroutine()
    43.     {
    44.         Debug.Log("LoadNextSceneCoroutine");
    45.      
    46.         // Wait for a few frames for the app to process everything
    47.         yield return null;
    48.         yield return new WaitForEndOfFrame();
    49.         yield return null;
    50.         yield return new WaitForEndOfFrame();
    51.  
    52.         Debug.Log("LOAD SCENE HERE");
    53.     }
    54.  
    55.     void OnApplicationFocus(bool hasFocus)
    56.     {
    57.         if (hasFocus)
    58.         {
    59.             if (RequestingCameraPermissionNow)
    60.             {
    61.                 RequestingCameraPermissionNow = false;
    62.          
    63.                 if (Application.HasUserAuthorization(UserAuthorization.WebCam))
    64.                 {
    65.                     Debug.Log("Webcam Allowed");
    66.                 }
    67.                 else
    68.                 {
    69.                     Debug.Log("Webcam not Allowed");
    70.                 }
    71.  
    72.                 StartCoroutine(RequestMicrophonePermissionCoroutine());
    73.             }
    74.             else if (RequestingMicrophonePermissionNow)
    75.             {
    76.                 RequestingMicrophonePermissionNow = false;
    77.                 if (Application.HasUserAuthorization(UserAuthorization.Microphone))
    78.                 {
    79.                     Debug.Log("Microphone Allowed");
    80.                 }
    81.                 else
    82.                 {
    83.                     Debug.Log("Microphone not Allowed");
    84.                 }
    85.  
    86.                 StartCoroutine(LoadNextSceneCoroutine());
    87.             }
    88.         }
    89.     }
    90. }
     
  7. puzzlekings

    puzzlekings

    Joined:
    Sep 6, 2012
    Posts:
    404