web-dev-qa-db-ja.com

iOS:カメラへのアクセスをリクエストする

正常に動作するQRコードスキャナーを備えたアプリがありますが、iOS8ではカメラへのデフォルトのアクセスは「拒否」です。したがって、設定に移動し、カメラを使用するためにアプリに手動でアクセスを許可する必要があります。 「このアプリにカメラを使用するためのアクセス権を付与しますか?」のようなプロンプトを作成するにはどうすればよいですか?

これは、カメラのアクセス許可をチェックし、ユーザーが許可を与えていない場合はアクセス許可を要求するコードのサンプルです。ただし、権限を付与するためのリンクは表示されず、最終的にはUIAlertViewのみが表示されます。テストするとステータスは実際に拒否されますが、許可を求めていない理由はありますか?ありがとう!

また、私は#import AVFoundation/AVFoundation.hを持っているので、それは問題ではありません。

-(void) checkCameraAuthorization {

AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

if(status == AVAuthorizationStatusAuthorized) { // authorized
    NSLog(@"camera authorized");
}
else if(status == AVAuthorizationStatusDenied){ // denied
    if ([AVCaptureDevice respondsToSelector:@selector(requestAccessForMediaType: completionHandler:)]) {
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            // Will get here on both iOS 7 & 8 even though camera permissions weren't required
            // until iOS 8. So for iOS 7 permission will always be granted.

            NSLog(@"DENIED");

            if (granted) {
                // Permission has been granted. Use dispatch_async for any UI updating
                // code because this block may be executed in a thread.
                dispatch_async(dispatch_get_main_queue(), ^{
                    //[self doStuff];
                });
            } else {
                // Permission has been denied.
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Not Authorized" message:@"Please go to Settings and enable the camera for this app to use this feature." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
                [alert show];
            }
        }];
    }
}
else if(status == AVAuthorizationStatusRestricted){ // restricted
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Not Authorized" message:@"Please go to Settings and enable the camera for this app to use this feature." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alert show];
}
else if(status == AVAuthorizationStatusNotDetermined){ // not determined

    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
        if(granted){ // Access has been granted ..do something
            NSLog(@"camera authorized");
        } else { // Access denied ..do something
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Not Authorized" message:@"Please go to Settings and enable the camera for this app to use this feature." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alert show];
        }
    }];
}
}
14
MSU_Bulldog

アプリはすでにカメラへのアクセスを拒否されているようです。この場合、再度プロンプトを表示することはできません。インストールごとに1回だけユーザーにアクセスを促すことができます。その後、ユーザーを設定に誘導する必要があります。

これでカメラを有効にできる設定にユーザーを送信します(iOS8の場合)。

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

テストしている場合は、電話からアプリを削除してから、インストールして再実行してください。これにより、AVAuthorizationStatusNotDetermined状態に戻ります。

16
Dan Loughney

for Swift 3:

UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)

詳細については、このビデオをご覧ください https://www.youtube.com/watch?v=Btd1XH-gHKM

2