Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

iOSでカメラと写真の利用許可の確認方法[AVCaptureDevice]

More than 5 years have passed since last update.

カメラプライバシーの確認

AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

switch (status) {
    case AVAuthorizationStatusAuthorized:
        // プライバシー設定でカメラの使用が許可されている場合
        break;
    case AVAuthorizationStatusDenied:
        // プライバシー設定でカメラの使用が禁止されている場合
        break;
    case AVAuthorizationStatusRestricted:
        // 機能制限の場合とあるが、実際にこの値をとることがなかった
        break;
    case AVAuthorizationStatusNotDetermined:
        // 初回起動時に許可設定を促すダイアログが表示される
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            if (granted) {
                // 許可された場合の処理
                dispatch_async(dispatch_get_main_queue(), ^{
                    // do something
                });
            } else {
                // 許可してもらえない場合
                dispatch_async(dispatch_get_main_queue(), ^{
                    // do something
                });
            }
        }];
        break;
    default:
        break;
}

写真の利用制限の確認方法

ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];

switch (status) {
    case ALAuthorizationStatusAuthorized:
        // 写真へのアクセスが許可されている状態
        break;
    case ALAuthorizationStatusNotDetermined:
    {
        // 初回起動時に許可設定を促すダイアログが表示される
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        [library enumerateGroupsWithTypes:ALAssetsGroupAll
                               usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
                                   // 許可された場合
                                   dispatch_async(dispatch_get_main_queue(), ^{
                                       // do something
                                   });
                                }
                              failureBlock:^(NSError *error) {
                                   // 許可してもらえない場合
                                   dispatch_async(dispatch_get_main_queue(), ^{
                                   });
                               }];
    }
        break;
    case ALAuthorizationStatusDenied:
        // プライバシーで許可されていない状態
        break;
    case ALAuthorizationStatusRestricted:
       // 機能制限されている場合
        break;
    default:
        break;
}

更新:
[ALAssetsLibrary authorizationStatus]はiOS9以降Deprecated。

iOS9以降[PHPhotoLibrary authorizationStatus]をお勧めです。
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusAuthorized) {
     // Access has been granted.
}
else if (status == PHAuthorizationStatusDenied) {
     // Access has been denied.
}
else if (status == PHAuthorizationStatusNotDetermined) {
     // Access has not been determined.
     [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
         if (status == PHAuthorizationStatusAuthorized) {
             // Access has been granted.         
         }
         else {
             // Access has been denied.
         }
     }];  
}
else if (status == PHAuthorizationStatusRestricted) {
     // Restricted access - normally won't happen.
}

Don't forget to #import <Photos/Photos.h>
許可ダイアログを出す
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
         if (status == PHAuthorizationStatusAuthorized) {
             //
         }
}];

iOS8から設定画面

NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:settingsURL];

参考:
http://mpon.hatenablog.com/entry/2015/03/15/005615
http://dev.classmethod.jp/references/ios8-photo-kit-7/
http://stackoverflow.com/questions/26595343/determine-if-the-access-to-photo-library-is-set-or-not-ios-8

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away