6

I have developed an app with Vuforia for iOS and Android, when the app is run for the first time, the user is prompted to authorize the use of the device's camera.

If the user does not authorize the camera, when the user navigates to the "capture" scene, the camera view is black.

I have looked at the Untiy3D Documentation and found the Application.HasUserAuthorization class, but if I understand correctly, this only works on the Unity web player?

How can I check if the user has allowed the use of the camera in Unity3D (c#).

Any suggestions would be greatly appreciated! TIA!

Ronny vdb
  • 2,324
  • 5
  • 32
  • 74

2 Answers2

1

As a user on Android I get prompted when installing the apk that it will use the Camera, once I accept that is it. As a developer I don't need to check.

This is how I start the camera using Vuforia API.

 CameraDevice.Instance.Stop();
 CameraDevice.Instance.Init(CameraDevice.CameraDirection.CAMERA_FRONT);
 CameraDevice.Instance.Start();

CameraDevice is a Singleton that you can call.

apxcode
  • 7,696
  • 7
  • 30
  • 41
  • Thanks for your reply @FunctionR, but what happens if you don't accept when installing the APK? If I call the Singleton when the user has not allowed the camera, will they be prompted again? – Ronny vdb Oct 01 '14 at 06:27
  • If you don't accept when installing the apk then it doesn't install. – apxcode Oct 01 '14 at 06:58
  • I have not checked on Android yet, but in iOS it still installs the app even if the user does not allow the camera. I am looking for some way to check the camera permission at runtime – Ronny vdb Oct 01 '14 at 07:51
  • Very interesting. I must get a hold of an iOS device to see this. – apxcode Oct 01 '14 at 08:53
0

Application.RequestUserAuthorization apparently works for any PC or mobile device camera. I am using it and the NatCam plugin for Unity3D on iPhone with the following script to check / prompt for user camera permissions. The while() loop is to allow them to take their time giving permission.

int cameraPermission = 0;

// Use this for initialization
public override void Start () {

    if (!Application.HasUserAuthorization (UserAuthorization.WebCam)) {
        // request camera use
        Application.RequestUserAuthorization (UserAuthorization.WebCam);
    } else {
        cameraPermission = 1;
    }

    // if we don't have permission, wait until we do 
    while (cameraPermission != 1) {
        // wait a sec
        System.Threading.Thread.Sleep(1000);
        // if user enables preference
        if (Application.HasUserAuthorization (UserAuthorization.WebCam)) {
            // proceed to start app
            cameraPermission = 1;
        }
    }

    // Start base
    base.Start ();
}
ow3n
  • 5,974
  • 4
  • 53
  • 51
  • `Application.RequestUserAuthorization` doesn't work on devices – Daniel May 31 '18 at 03:20
  • @Daniel thanks for the down vote, but you should check your code. The code I have shared above is working in an app currently published on the app store/google play. – ow3n May 31 '18 at 11:26
  • tried yesterday with Unity 2017.4.3f1 on Android and didn't work, and if you check [here](https://forum.unity.com/threads/request-and-get-state-of-camera-permission.281858/) you'll notice that people say it is not made for this job. – Daniel May 31 '18 at 16:21
  • It worked for me in July 2017, a few months after those comments. What happens when you log those results? – ow3n May 31 '18 at 23:45
  • It says user has given webcam authorization even when I deny camera usage (on both Android and iOS) – Daniel May 31 '18 at 23:51
  • Hmm, it has been a year since I worked on this. But I remember I had to completely delete the app every time I built in order to properly test permissions. Otherwise it gave weird results. Hopefully that helps. – ow3n Jun 01 '18 at 01:37