アクセス許可の情報を取得する
カメラへのアクセスが許可に関する情報を取得する。
AVAuthorizationStatus vidioStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
AVAuthorizationStatusは次の値をとる。
- AVAuthorizationStatusNotDetermined
- ユーザーは許可も拒否もしていない
- AVAuthorizationStatusRestricted
- ユーザーはデバイスにアクセスできない
- AVAuthorizationStatusDenied
- ユーザーは明示的に拒否した
- AVAuthorizationStatusAuthorized
- ユーザーは明示的に許可した、または、許可は必要ない
アクセスの許可を求めるダイアログを表示する
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
}];
許可されたときはgrantedはYESに、拒否されたときはgrantedはNOになる。
設定アプリを開く
アクセスが拒否されているときは、設定アプリを開き、ユーザーにアクセスを許可してもらう。
NSURL* url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];
実際にアプリに組み込むイメージ。
checkPermissionAndDoSomething()を呼ぶと、権限を適切に処理してからdoSomething()を実行する。
- (void)checkPermissionAndDoSomething
{
if ([self hasAuthorization]) { // 権限を確認する
[self doSomething]; // 権限があるので実行する
}
[self requireAuthorization:^() { // 権限を要求する
if ([self hasAuthorization]) { // 許可を得られた
[self doSomething]; // 権限があるので実行する
} else {
// 許可されませんでした
}
}];
}
/**
@brief カメラの権限があるか
*/
- (bool)hasAuthorization
{
AVAuthorizationStatus videoStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
return (videoStatus == AVAuthorizationStatusAuthorized);
}
// コールバックの型
typedef void (^voidBlock)(void);
/**
@brief カメラの権限を要求する
*/
- (void)requireAuthorization:(voidBlock)callback
{
AVAuthorizationStatus videoStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
// カメラの権限があるとき
if (videoStatus == AVAuthorizationStatusAuthorized) {
callback();
return;
}
// カメラの権限を問い合わせていないとき
if (videoStatus == AVAuthorizationStatusNotDetermined) {
// カメラの初回権限要求
[self requireInitialAuthorization:callback];
return;
}
// 許可されていない権限があるとき
[self requireAuthorizationToOpenSettings:callback];
}
/**
@brief カメラの初回権限要求
*/
- (void)requireInitialAuthorization:(voidBlock)callback
{
NSString* title = @"アクセス権限の設定";
NSString* message = @"このアプリはカメラを使うので許可してください。";
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK"
style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
callback();
}];
}]];
[self presentViewController:alertController animated:YES completion:nil];
}
/**
@brief カメラの権限を設定するために設定画面を開く
*/
- (void)requireAuthorizationToOpenSettings:(voidBlock)callback
{
NSString* title = @"アクセス権限の設定";
NSString* message = @"カメラのアクセスを許可してください。";
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"設定する"
style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSURL* url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];
callback();
}]];
[self presentViewController:alertController animated:YES completion:nil];
}