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
yield return Application.RequestUserAuthorization(UserAuthorization.Microphone);
if (Application.HasUserAuthorization(UserAuthorization.Microphone)) {
selectedDevice = Microphone.devices[0].ToString();
var audioSource = GetComponent<AudioSource>()
audioSource.clip = null;
audioSource.loop = true; // Set the AudioClip to loop
audioSource.mute = false; // Mute the sound, we don't want the player to hear it
audioSource.clip = Microphone.Start(selectedDevice, true, 10, maxFreq);//Starts recording
while (!(Microphone.GetPosition(selectedDevice) > 0)) { } // Wait until the recording has started
audioSource.Play(); // Play the audio source!
}
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)
Thanks Paulius! Turns out that the Application.RequestUserAuthorization doesn’t work on iOS, and rather always returns false. Might be something for the Unity team to look at.
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.
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:
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?
@_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?
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.
@_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?
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.
@_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?
@_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.
@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.
I have tried your code fix by using a coroutine calling this :
IEnumerator GetMicrophone()
{
yield return Application.RequestUserAuthorization (UserAuthorization.Microphone);
if (Application.HasUserAuthorization (UserAuthorization.Microphone)) {
Debug.Log ("We received the mic");
//StartRecording
} else {
Debug.Log ("We encountered an error");
//Error
}
}
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!
the fix will available in 5.6.1p1 which should be out in a week or two. Another workaround would be to reference Unity - Scripting API: WebCamTexture anywhere in the project, so that the permissions code wouldn’t be disabled.
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.