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.
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
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.
#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.
@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:
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!
_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?