Checking permissions on Android and iOS

My app records audio, and I need to know if user granted such permission, or not. Is there any method in Unity to know if a permission has been granted?

I read here that Application.HasUserAuthorization works on iOS although is not documented. Can we trust it, at least for iOS?

EDIT: Application.HasUserAuthorization returns false on iOS, so it’s not reliable.

Any way to know if user authorized microphone access or not?

Android users give apps all required permissions when they install them.
You don’t need to check afterwards. If app was installed then you’ve been granted all required permissions.

But that’s what I know from android devving. I’m not sure if unity does anything different.

Since Android 6, you have to ask for permissions at runtime. Anyway, Android is not the problem, because I’m an Android developer and I can create a simple plugin that calls checkSelfPermission.

The problem is iOS. Application.HasUserAuthorization didn’t work for iOS.

I can’t believe that Unity is showing permission dialog, and there is no way knowing what the user answered. Really??

seen this?

Thanks. Yes, I’ve seen. In a similar way I think I could create a plugin for asking permission. But I don’t want to create a plugin for something Unity is already doing. I only want to know if permission is granted, I don’t want to ask again.

Here is how I solved for Android:

public class AndroidUtils{
    public static int RUNTIME_PERMISSIONS_MIN_SDK_LEVEL = 23;
    public static string PERMISSION_RECORD_AUDIO = "android.permission.RECORD_AUDIO";

    public static int GetSDKLevel() {
        var clazz = AndroidJNI.FindClass("android/os/Build$VERSION");
        var fieldID = AndroidJNI.GetStaticFieldID(clazz, "SDK_INT", "I");
        var sdkLevel = AndroidJNI.GetStaticIntField(clazz, fieldID);
        return sdkLevel;
    }

    public static AndroidJavaObject GetCurrentActivity(){
        return new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
    }

    public static bool RuntimePermissionGranted(string permission){
        if (GetSDKLevel() < RUNTIME_PERMISSIONS_MIN_SDK_LEVEL){
            return true;
        }

        return GetCurrentActivity().Call<int>("checkSelfPermission", new object[]{permission}) == 0;
    }
}

There is any way I can do anything similar for iOS?

1 Like

Hi there!

Is the iOS issue still unsolved? :o

Because I was trying to record audio, I assume that the user didn’t give record permission if recorded audio is 0 bytes. But I don’t know how to check for permission correctly on iOS. Would be great if anybody could help us.