Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

Request and get state of camera permission

Discussion in 'iOS and tvOS' started by Prodigga, Nov 24, 2014.

  1. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,030
    Hey guys

    There does not appear to be a way to ask the user for camera permissions in iOS?

    The dialogue for the permission will pop up by itself when you try to Play() the WebCamTexture for the first time. If the user accepts the permission request, then the camera works just fine. If they deny it, we get a 'empty' WebCamTexture image.

    I understand that after the user has denied the permission there is no way to ask them again. The user needs to go into his device settings and manually enable the camera permissions. However, there is no way for me to check whether or not the user has accepted or denied the permissions.

    If the user denied the camera permissions, I am going to display an ingame popup instructing them to enable the camera. Currently there is no way to know whether or not the camera is running correctly.
     
    ina likes this.
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    2,981
    When you create the WebCamTexture, check if its 'empty' as you put it. If it's 'empty' then you know for sure that the user disabled it and needs to go to settings.
     
  3. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,030
    Sorry for not being too clear on the 'empty' thing! So, when I say 'empty', I just mean it is one big black texture.It would involve reading the pixels on the entire texture and hoping that the camera is not infact blocked and actually returning a black texture. Slow and not very reliable :D

    Anyway, I have writen a native iOS plugin that lets me check the apps 'camera permission' status so I can act on it accordingly. I can share it here if anyone has the same issue. :)
     
  4. fafase

    fafase

    Joined:
    Jul 3, 2012
    Posts:
    158
    idbrii likes this.
  5. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,030
    No thats only for web player, I've already tried it to be sure. :)

    The solution was to write a plugin.
     
    ina likes this.
  6. z_blush

    z_blush

    Joined:
    Apr 16, 2014
    Posts:
    8
    Prodigga, I'd love to see what you did. I've been told by other engineers that there isn't a way to check the status without invoking the popup....

    Thanks in advance!
     
  7. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,465
    what unity version did you try? on latest 4.6 (and 5.0 sure) this api works on ios
     
    idbrii likes this.
  8. z_blush

    z_blush

    Joined:
    Apr 16, 2014
    Posts:
    8
    Yup, this

    AVAuthorizationStatus authStatus =[AVCaptureDevice authorizationStatusForMediaType:
    AVMediaTypeVideo];


    works fine in iOS7.

    Edit: in case it's not clear, the above is Objective-C and needs to go in a plugin.
     
  9. smithmj5

    smithmj5

    Joined:
    May 24, 2013
    Posts:
    125
    I would be very interested in what you wrote, since I'm having this issue as well. Cheers!
     
  10. fafase

    fafase

    Joined:
    Jul 3, 2012
    Posts:
    158
    iOSNative.h

    Code (CSharp):
    1. #ifdef __cplusplus
    2. extern "C" {
    3. #else
    4. @interface iOSNative: NSObject
    5. #endif
    6.     void sd_camera_permission();
    7. #ifdef __cplusplus
    8. }
    9. #else
    10. @end
    11. #endif
    Then in the iOSNative.m

    Code (CSharp):
    1.  
    2. #include "sd_native.h"
    3. #import <Social/Social.h>
    4. #import <Foundation/NSException.h>
    5. #import <FacebookSDK/FacebookSDK.h>
    6. #import "DocumentSharer.h"
    7. #import <AVFoundation/AVFoundation.h>
    8. #import <UIKit/UIKit.h>
    9. @implementation sd_native {}
    10. BOOL CameraCheckDone = NO;
    11. void sd_camera_permission()
    12. {
    13.     if ([AVCaptureDevice respondsToSelector:@selector(requestAccessForMediaType:completionHandler:)]) {
    14.     // Completion handler will be dispatched on a separate thread
    15.     [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
    16.         if (YES == granted) {
    17.             // User granted access to the camera, continue with app launch
    18.         }
    19.         else {
    20.             // User denied camera access
    21.             // warn the user that the app requires camera access
    22.             // and ideally provide some guidance about the device Settings
    23.         }
    24.        
    25.         // Here mark that the camera access check has been completed
    26.         // (no matter if the 'granted' is YES or NO)
    27.         CameraCheckDone = YES;
    28.     }];
    29.     }
    30.     else {
    31.     // iOS < 7 (camera access always OK)
    32.    
    33.         CameraCheckDone = YES;
    34.         // Continue with app launch...
    35.     }
    36. }
    And finally a iOSBridge.cs

    Code (CSharp):
    1. public class NativeBridge
    2. {
    3. #if UNITY_IOS
    4.     [DllImport ("__Internal")] extern static private void sd_camera_permission();
    5.  
    6.                public void static CameraAuthorization(){
    7.                sd_camera_permission();
    8.         }
    9. #endif
    10. }
    11.  
    This will pop up the native alert window if the authorization is not found.
     
    a_d_69 likes this.
  11. TNTdev

    TNTdev

    Joined:
    Dec 28, 2016
    Posts:
    1
    Can you Please share the plugin that you used to detect user grant permissions for the camera
     
  12. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,030
    Sorry partner, I no longer have access to the code, it was for my previous job.
     
    TNTdev likes this.
  13. EvansT

    EvansT

    Joined:
    Jan 22, 2015
    Posts:
    22
    @Alexey I tried it on Unity 4.6.9f1, Application.RequestUserAuthorization (UserAuthorization.WebCam) has no effect and HasUserAuthorization(UserAuthorization.WebCam) returns true even if the authorization is turned off in settings. Could you confirm if this is supposed to work in maybe 4.7?
     
  14. MihajloNen

    MihajloNen

    Joined:
    Dec 7, 2015
    Posts:
    16
    Hi everyone, I am sorry for reopening such an old post. Have you found a better solution or is still the plugin the only way to check the permission given by the user? When following fafase's advice I get the following error:

    duplicate symbol _sd_camera_permission in:

    /Users/myname/Library/Developer/Xcode/DerivedData/Unity-iPhone-ermpajkcpjlzemgbxljvzihqhtfd/Build/Intermediates.noindex/Unity-iPhone.build/Debug-iphoneos/Unity-iPhone.build/Objects-normal/armv7/iOSNative-533395CB661FA45F.o

    /Users/myname/Library/Developer/Xcode/DerivedData/Unity-iPhone-ermpajkcpjlzemgbxljvzihqhtfd/Build/Intermediates.noindex/Unity-iPhone.build/Debug-iphoneos/Unity-iPhone.build/Objects-normal/armv7/iOSNative-6EF5AE22E77534BA.o

    duplicate symbol _CameraCheckDone in:

    /Users/myname/Library/Developer/Xcode/DerivedData/Unity-iPhone-ermpajkcpjlzemgbxljvzihqhtfd/Build/Intermediates.noindex/Unity-iPhone.build/Debug-iphoneos/Unity-iPhone.build/Objects-normal/armv7/iOSNative-533395CB661FA45F.o

    /Users/myname/Library/Developer/Xcode/DerivedData/Unity-iPhone-ermpajkcpjlzemgbxljvzihqhtfd/Build/Intermediates.noindex/Unity-iPhone.build/Debug-iphoneos/Unity-iPhone.build/Objects-normal/armv7/iOSNative-6EF5AE22E77534BA.o

    ld: 2 duplicate symbols for architecture armv7

    clang: error: linker command failed with exit code 1 (use -v to see invocation)


    I would appreciate any help in here. TY
     
  15. MihajloNen

    MihajloNen

    Joined:
    Dec 7, 2015
    Posts:
    16

    Is it possible for you to lead me to a page with a good tutorial about your plugins idea or could you make a precise step by step guide? I really appreciate any help, because I am stuck with this problem. I simply want to get a boolian which says if the user has given access permissions to the iOS camera or not. TY in advance!
     
    airespt likes this.
  16. CoryButler

    CoryButler

    Joined:
    Apr 19, 2013
    Posts:
    2
  17. Fillmore

    Fillmore

    Joined:
    Feb 14, 2018
    Posts:
    8
    Can you link or post the code?
     
  18. Fillmore

    Fillmore

    Joined:
    Feb 14, 2018
    Posts:
    8
    Undefined symbols for architecture arm64:

    "__verifyPermission", referenced from:

    _Permissions__verifyPermission_m2751257267 in Bulk_Assembly-CSharp_2.o

    (maybe you meant: _Permissions__verifyPermission_m2751257267)

    ld: symbol(s) not found for architecture arm64

    clang: error: linker command failed with exit code 1 (use -v to see invocation)

    EDIT: Make sure to include the 3 files in Plugins/iOS - Works perfectly!
    @CoryButler Why did you make it a string == "true" and == "false" ?
    Can you just make it a boolean?
     
    Last edited: Mar 16, 2018
  19. jtbentley

    jtbentley

    Joined:
    Jun 30, 2009
    Posts:
    1,397
    This got me out of a bind, great work :)
     
  20. dhanrajsinh24

    dhanrajsinh24

    Joined:
    May 8, 2014
    Posts:
    57
    @CoryButler your plugin does not show a permission dialog.
     
  21. Basic50

    Basic50

    Joined:
    Dec 30, 2013
    Posts:
    5
    I love guy! Thank you very much!
     
  22. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    528
    @CoryButler does the permission has to be ask every time or is there a way to check if the permission has already been granted (and is indeed granted) ?

    Thanks
     
    viswas163 likes this.
  23. AM-Dev

    AM-Dev

    Joined:
    Aug 5, 2015
    Posts:
    31
    @CoryButler, is it possible to trigger multiple requests in the App?
    So, when the user declines the first popup, he could click the button again and the popup is shown again.
    Or is there no way to do that and therefore you're changing the text on the button ("Please active camera access in Settings.")?
     
  24. macagu

    macagu

    Joined:
    Sep 30, 2012
    Posts:
    9
    I was getting a black texture after authorizing the WebCam. "Camera2: Error opening CameraDevice 4" showed on logcat (this only happens the first time after installing).

    Instead of checking for black pixels, i checked for webcamTexture.didUpdateThisFrame (which returns false all the time the texture is black). If no frame is updated after a couple of seconds, then i created a new texture. This solves the issue.

    I'm using Unity 2019.1.6f1
     
  25. idbrii

    idbrii

    Joined:
    Aug 18, 2014
    Posts:
    51
    I stumbled on this thread when trying to initiate the iOS permission request and check for permissions. Turns out the Unity API works on 2019.4.7:

    https://docs.unity3d.com/ScriptReference/Application.RequestUserAuthorization.html

    I read somewhere else you also needed a (null) reference to a WebCamTexture to ensure some native code got loaded, but I haven't tested whether it works without it.

    (I'm reading from the camera with arfoundation, so I only know that the permissions part works.)
     
  26. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,465
    yes this is an expected way if you want to ask about it upfront

    as i understand you do it from "outside" unity so indeed using this api is THE correct way (if you would use WebCamTexture it should ask for permission automatically iirc)
     
    chrisgoat likes this.
  27. carlastreckwall

    carlastreckwall

    Joined:
    May 30, 2018
    Posts:
    5
    I am still having an issue here, maybe someone can help? Working in Unity 2020.1.1 and ARFoundation 3.1.3. I want to create a UI element that only pops up when the user denies camera permission. The native notification to ask for camera permission works fine, just when I try to add a script to verify the status, it always shows my alert even when the user already granted access to the camera. Any hints?

    using System.Collections;
    using UnityEngine;
    using UnityEngine.iOS;

    public class CameraPermissionChecker : MonoBehaviour
    {
    public GameObject cameraPermissionAlert;

    IEnumerator Start()
    {
    yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
    if (Application.HasUserAuthorization(UserAuthorization.WebCam))

    {
    Debug.Log("webcam found");
    cameraPermissionAlert.SetActive(false);
    }
    else
    {
    Debug.Log("no webcam found");
    cameraPermissionAlert.SetActive(true);
    }
    }
    }
     
    jade_unity238 likes this.
unityunity