Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

iOS App Never Asks for Microphone Permissions

Discussion in 'iOS and tvOS' started by DrakenDev, Jan 10, 2017.

  1. DrakenDev

    DrakenDev

    Joined:
    Jul 20, 2012
    Posts:
    11
    Hi, I'm struggling to get my app to gain microphone permissions.
    Once compiled and installed on an iPhone, it never asks the user for microphone access.
    Obviously, I therefore dont get any sound to my app.
    As far as I know, I'm doing everything correctly.
    This is how I'm asking for Microphone permissions
    Code (CSharp):
    1. yield return Application.RequestUserAuthorization(UserAuthorization.Microphone);
    2. if (Application.HasUserAuthorization(UserAuthorization.Microphone)) {
    3.      selectedDevice = Microphone.devices[0].ToString();
    4.      var audioSource = GetComponent<AudioSource>()
    5.      audioSource.clip = null;
    6.      audioSource.loop = true; // Set the AudioClip to loop
    7.      audioSource.mute = false; // Mute the sound, we don't want the player to hear it
    8.      audioSource.clip = Microphone.Start(selectedDevice, true, 10, maxFreq);//Starts recording
    9.      while (!(Microphone.GetPosition(selectedDevice) > 0)) { } // Wait until the recording has started
    10.      audioSource.Play(); // Play the audio source!
    11. }
    The reason for Microphone permissions have been set in the Build Settings.

    Unity Documentation says that it will automatically add Microphone Permissions to the info.plist if there are ANY calls to Microphone. Does my script therefore perhaps need to be in a specific folder like plugins?

    (And as a side note, this code still plays whatever the microphone records, through the speaker. Not sure how to stop that)
     
    Last edited: Jan 10, 2017
  2. DrakenDev

    DrakenDev

    Joined:
    Jul 20, 2012
    Posts:
    11
    Bump... Still need help :/
     
  3. _Paulius

    _Paulius

    Mobile Developer Unity Technologies

    Joined:
    Jul 8, 2014
    Posts:
    173
  4. DrakenDev

    DrakenDev

    Joined:
    Jul 20, 2012
    Posts:
    11
    Cromfeli likes this.
  5. Cromfeli

    Cromfeli

    Joined:
    Oct 30, 2014
    Posts:
    197
    It would be very good to have explicit control when the permissions are being asked. Today users are very sensitive to permissions and uncontrolled is bad experience.
     
  6. _Paulius

    _Paulius

    Mobile Developer Unity Technologies

    Joined:
    Jul 8, 2014
    Posts:
    173
    So it turns out that our docs are wrong (I'll look into fixing that), Application.RequestUserAuthorization should work on iOS and it's currently the only platform that supports it. I've tried this running this on iOS 10:

    Code (CSharp):
    1. yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
    2. if(Application.HasUserAuthorization(UserAuthorization.WebCam))
    3. {
    4.    //StartRecording
    5. }
    6. else
    7. {
    8.    //Error
    9. }
    10.  
    11. yield return Application.RequestUserAuthorization(UserAuthorization.Microphone);
    12. if(Application.HasUserAuthorization(UserAuthorization.Microphone))
    13. {
    14.    //StartRecording
    15. }
    16. else
    17. {
    18.    //Error
    19. }
    and in both cases I got the permission dialog and was able to use the mic or camera. @DrakenDev is that code snippet in a coroutine? If so could you please submit a bug report ('Help->Report a Bug' in the editor) with a project that uses this script so that we could have a look?
     
    Cromfeli likes this.
  7. plysaght47

    plysaght47

    Joined:
    Jul 2, 2015
    Posts:
    21
    @_Paulius I've been running into a similar issue building to iOS via Unity cloud. My application requires camera and photo access and runs smoothly on Android. When I test the app on iOS, the application crashes when trying to open the camera.

    When I install my app on iOS, it doesn't ask for any permissions at all which I would guess is the issue. So, what exactly do I need to do to ask for permission to the camera on iOS?

    Just include a similar script to the one you posted? Or something else?
     
  8. _Paulius

    _Paulius

    Mobile Developer Unity Technologies

    Joined:
    Jul 8, 2014
    Posts:
    173
    It might be crashing because cameraUsageDescription is not specified in player settings. In iOS 10 Apple requires the app to show a description string when requesting permission for camera usage and the app usually crashes if it's not specified and the camera API is used.

    I'm not sure what happening if the app doesn't crash and doesn't ask for permission. Just calling WebCamTexture.Play should show the permission dialog (it's just that there is no way to know if user declined in this case). But you could try using the snippet from the post above and check whether it helps.
     
    Cromfeli likes this.
  9. plysaght47

    plysaght47

    Joined:
    Jul 2, 2015
    Posts:
    21
    @_Paulius I added the script you posted above, and the camera permission prompt worked. I was able to access the camera and take a photo. However, when I try to access the photo gallery, the app crashes.

    So is there a similar authorization script for accessing the photo/video gallery on iOS?

    Thanks.
     
  10. _Paulius

    _Paulius

    Mobile Developer Unity Technologies

    Joined:
    Jul 8, 2014
    Posts:
    173
    Yes, accessing the photo library seems to also require an usage description string (NSPhotoLibraryUsageDescription) in info.plist. Since we don't provide a way to access the library from our C# api we there is no way to set that field from Unity. Are you using a native plugin to access it? Cause in that case the plugin should probably update the plist file wen building the project.

    Assuming the crash is caused by the permission issue you should able to fix it by manually adding the 'Privacy - Photo Library Usage Description' field to your xcode project info.plist file.
     
  11. plysaght47

    plysaght47

    Joined:
    Jul 2, 2015
    Posts:
    21
    @_Paulius Thanks. This is actually my first experience building to an iOS device. I actually don't own a mac so I've been using macinthecloud and Unity cloud build so I am at a bit of a disadvantage here.

    I tried adding in the NSPhotoLibraryUsageDescription to one of my info.plist files but I'm not sure I did it correctly. I am also facing an issue getting the Vuforia plugin to work in...it also causes a crash.

    So I'm definitely facing some plugin issues...does it matter that there are multiple info.plist files currently? Or does Unity Cloud build automatically combine them?

    Thanks for the help.
     
  12. plysaght47

    plysaght47

    Joined:
    Jul 2, 2015
    Posts:
    21
    @_Paulius I had to add the following script and accessing the photo library is now working.

    using UnityEngine;
    using UnityEditor;
    using UnityEditor.Callbacks;
    using UnityEditor.iOS.Xcode;
    using System.IO;
    using System.Collections.Generic;

    public class campermit : MonoBehaviour {

    #if UNITY_IOS
    [PostProcessBuild]
    static void OnPostprocessBuild(BuildTarget buildTarget, string path)
    {
    // Read plist
    var plistPath = Path.Combine(path, "Info.plist");
    var plist = new PlistDocument();
    plist.ReadFromFile(plistPath);

    // Update value
    PlistElementDict rootDict = plist.root;
    rootDict.SetString("NSCameraUsageDescription", "Used for taking selfies");

    // Write plist
    File.WriteAllText(plistPath, plist.WriteToString());
    }
    #endif
    }
     
  13. EvansT

    EvansT

    Joined:
    Jan 22, 2015
    Posts:
    22
    @_Paulius Does it work on iOS on Unity 4.6.9?
     
  14. dustinmerrell

    dustinmerrell

    Joined:
    Oct 6, 2014
    Posts:
    11
    @_Paulius
    I am on OS 10.12.4, Xcode 8.3.2, Unity 5.6.0f3
    I have set Application.RequestUserAuthorization(UserAuthorization.Microphone);
    I also have the info.plst Privacy string set.
    Since updating to Unity 5.6 I am no longer able to get the microphone permission prompt. I always get the permssion is false. Did not have this issue on 5.4.
     
    mrodil likes this.
  15. _Paulius

    _Paulius

    Mobile Developer Unity Technologies

    Joined:
    Jul 8, 2014
    Posts:
    173
    @dustinmerrell that seems to be a bug we had recently introduced, thanks for pointing it out. An '
    UNITY_USES_WEBCAM' define got added to iOS trampoline to avoid linking camera code when it's not used, but it seems that all RequestUserAuthorization code is behind it so it just fails in all cases if the camera is not used in any script.

    We'll fix it in a future patch release, but for now you can workaround it by going to 'Classes/Unity/AVCapture.mm' in you xcode project and removing '
    UNITY_USES_WEBCAM' in lines 21 and 37.
     
  16. StargazerCS

    StargazerCS

    Joined:
    Feb 2, 2017
    Posts:
    1
    @_Paulius

    I have tried your code fix by using a coroutine calling this :

    Code (CSharp):
    1. IEnumerator GetMicrophone()
    2.     {
    3.         yield return Application.RequestUserAuthorization (UserAuthorization.Microphone);
    4.         if (Application.HasUserAuthorization (UserAuthorization.Microphone)) {
    5.             Debug.Log ("We received the mic");
    6.             //StartRecording
    7.         } else {
    8.             Debug.Log ("We encountered an error");
    9.             //Error
    10.         }
    11.     }
    I removed UNITY_USES_WEBCAM on lines 21 and 37 and am still running into the issue that iOS crashes when I try to get an authorization from the phone or tablet.

    This is literally all my small app is doing and it simply crashes each time. Please help me ASAP!

    iOS Version : 10.0+
    Unity Version : 5.6.0f3
     
  17. _Paulius

    _Paulius

    Mobile Developer Unity Technologies

    Joined:
    Jul 8, 2014
    Posts:
    173
    MrEsquire likes this.
  18. RMGK

    RMGK

    Joined:
    Sep 30, 2011
    Posts:
    59
    @_Paulius Does this go for Application.HasUserAuthorization as well?

    ... Just checked, it worked for me 5.6.1p3
     
    Last edited: Jun 22, 2017
  19. _Paulius

    _Paulius

    Mobile Developer Unity Technologies

    Joined:
    Jul 8, 2014
    Posts:
    173
    Yes, that's correct, the fix applies to both.
     
    RMGK likes this.
  20. ChiuGa

    ChiuGa

    Joined:
    Aug 24, 2017
    Posts:
    5
    Hi,
    Application.HasUserAuthorization and Application.RequestUserAuthorization are working in Unity 2017.1.0f3?
    I have tried the both in Unity 2017.1.0f3 and iOS 11, but I didn't get the permission dialog and I always got the permission is false.

    Thanks for the help.
     
  21. CarlosBMcRoy

    CarlosBMcRoy

    Joined:
    Aug 18, 2017
    Posts:
    2
    That's right.I am a starter too and couldn't figure out this issue. Could you please share some guide?
     
  22. _Paulius

    _Paulius

    Mobile Developer Unity Technologies

    Joined:
    Jul 8, 2014
    Posts:
    173
    @ChiuGa Application.RequestUserAuthorization only works if WebCamTexture, Microphone or ReplayKit are referenced anywhere in the Unity project (in this case it seems to work fine for us both on iOS 10 && 11).

    Could you check what are the values of 'UNITY_USES_WEBCAM' and 'UNITY_USES_MICROPHONE' in you Xcode project (these are defined in Clasess/Preprocessor.h)?
     
  23. justdizzy

    justdizzy

    Joined:
    Mar 8, 2016
    Posts:
    63
    So when will HasUserAuthorization() and RequestUserAuthorization() work for Android? Is Unity working on that or is that something that we will have to build ourselves/use 3rd party plugins for?
     
  24. justdizzy

    justdizzy

    Joined:
    Mar 8, 2016
    Posts:
    63
    For those where HasUserAuthorization() and RequestUserAuthorization() are not working on iOS, I don't know if this could cause it, but make sure you have "Camera Usage Description" or "Microphone Usage Description" set in Player Settings. I just verified that the microphone request methods work on iOS 10 and 11, in an app build in Unity 2017.1p5.
     
    Cfirzi likes this.
  25. _Paulius

    _Paulius

    Mobile Developer Unity Technologies

    Joined:
    Jul 8, 2014
    Posts:
    173
    @justdizzy currently we aren't working on this for Android, but I added it to our backlog. Don't know when it will be available, though.
     
  26. justdizzy

    justdizzy

    Joined:
    Mar 8, 2016
    Posts:
    63
    :( I repurposed another plugin to solve this.
     
  27. jdgauchat

    jdgauchat

    Joined:
    Jun 24, 2017
    Posts:
    18
    Hi. I'm working on an ARKit project and wanted to ask for authorization to use the camera before showing the ARKit scene. I used this code but I never get the dialog. I thought that there was a conflict with ARKit, so I created an empty project, added an empty object with only this code and I didn't get the dialog either. The following is the code I'm using:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Controller : MonoBehaviour {
    7.  
    8.     IEnumerator Start () {
    9.         yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
    10.         if (Application.HasUserAuthorization(UserAuthorization.WebCam)) {
    11.             print("Authorized");
    12.         } else {
    13.             print("Not Authorized");
    14.         }
    15.     }
    16. }
    17.  
    I always get the message "Not Authorized" on the console, but no dialog box. I have the Usage Description message set in info.plist and my version of Unity is 2018.2.8f1. I've been trying the whole day. Please, let me know if there is something I forgot or any possible solution. Thanks!
     
  28. _Paulius

    _Paulius

    Mobile Developer Unity Technologies

    Joined:
    Jul 8, 2014
    Posts:
    173
    @jdgauchat currently Application.RequestUserAuthorization only works if the WebCamTexture class is used somewhere in the project as otherwise we can't determine whether the camera is actually used by the app in buildtime (native plugins can be added after the project is already exported from Unity). And including camera authorization calls if they are not actually used might cause issues when submitting the app to the App Store.

    You should be able to work around this by referencing WebCamTexture in any script in your project (we're planning implement a better solution in the future).
     
    AM-Dev, Archviz3d and Cromfeli like this.
  29. jdgauchat

    jdgauchat

    Joined:
    Jun 24, 2017
    Posts:
    18
    Thanks! I decided to implement a plugin I found in the forum. I can't remember the author, but it works like a charm. I studied the code and I think I won't have any problems that way. I hope Unity provides a better solution in the future. Thanks again for your response!
     
  30. Dover8

    Dover8

    Joined:
    Aug 20, 2010
    Posts:
    92
    Would be good to know if an updated solution will come with the ARFoundation/ARKit/ARCore packages. It would make sense that if any of these packages are in use, then the permission request code should work and be included without the need for WebcamTexture references.
     
    R0man and AM-Dev like this.
  31. R0man

    R0man

    Joined:
    Jul 10, 2011
    Posts:
    77
    Wait a minute. You can build an ARFoundation iOS app without the application ever asking for camera permissions? When would that ever be necessary? Just include a dummy reference to WebCamTexture as boilerplate in all ArFoundation projects Unity. For fudge sake.
     
    Last edited: Aug 5, 2019
unityunity