Request and get state of camera permission

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.

1 Like

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.

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 :smile:

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. :slight_smile:

Does thos http://docs.unity3d.com/ScriptReference/Application.RequestUserAuthorization.html would be a solution?

1 Like

No thats only for web player, I’ve already tried it to be sure. :slight_smile:

The solution was to write a plugin.

1 Like

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!

what unity version did you try? on latest 4.6 (and 5.0 sure) this api works on ios

1 Like

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.

I would be very interested in what you wrote, since I’m having this issue as well. Cheers!

iOSNative.h

#ifdef __cplusplus
extern "C" {
#else
@interface iOSNative: NSObject
#endif
    void sd_camera_permission();
#ifdef __cplusplus
}
#else
@end
#endif

Then in the iOSNative.m

#include "sd_native.h"
#import <Social/Social.h>
#import <Foundation/NSException.h>
#import <FacebookSDK/FacebookSDK.h>
#import "DocumentSharer.h"
#import <AVFoundation/AVFoundation.h>
#import <UIKit/UIKit.h>
@implementation sd_native {}
BOOL CameraCheckDone = NO;
void sd_camera_permission()
{
    if ([AVCaptureDevice respondsToSelector:@selector(requestAccessForMediaType:completionHandler:)]) {
    // Completion handler will be dispatched on a separate thread
    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
        if (YES == granted) {
            // User granted access to the camera, continue with app launch
        }
        else {
            // User denied camera access
            // warn the user that the app requires camera access
            // and ideally provide some guidance about the device Settings
        }
       
        // Here mark that the camera access check has been completed
        // (no matter if the 'granted' is YES or NO)
        CameraCheckDone = YES;
    }];
    }
    else {
    // iOS < 7 (camera access always OK)
   
        CameraCheckDone = YES;
        // Continue with app launch...
    }
}

And finally a iOSBridge.cs

public class NativeBridge
{
#if UNITY_IOS
    [DllImport ("__Internal")] extern static private void sd_camera_permission();

               public void static CameraAuthorization(){
               sd_camera_permission();
        }
#endif
}

This will pop up the native alert window if the authorization is not found.

1 Like

Can you Please share the plugin that you used to detect user grant permissions for the camera

Sorry partner, I no longer have access to the code, it was for my previous job.

1 Like

@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?

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

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!

1 Like

Plugin to Request Camera Permission with a Callback

I have created a plugin that requests iOS camera permission and uses a callback to notify Unity of the result.
These is a usage sample included.

9 Likes

Can you link or post the code?

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?

This got me out of a bind, great work :slight_smile:

@CoryButler your plugin does not show a permission dialog.