LoginSignup
100
92

More than 5 years have passed since last update.

【iOS】アプリに各アクセス権限が付与されているか確認する方法

Last updated at Posted at 2015-05-01

備忘録代わりに書きます。

カメラ、カメラロール、マイク、プッシュ通知の4つです

カメラ

AVFoundationを使います。

import AVFoundation

let status = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)

if status == AVAuthorizationStatus.Authorized {
    // アクセス許可あり
} else if status == AVAuthorizationStatus.Restricted {
    // ユーザー自身にカメラへのアクセスが許可されていない
} else if status == AVAuthorizationStatus.NotDetermined {
    // まだアクセス許可を聞いていない
} else if status == AVAuthorizationStatus.Denied {
    // アクセス許可されていない
}

マイク

カメラと一緒です。

AVMediaTypeVideoAudioかの違いになります。

import AVFoundation

let status = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeAudio)

if status == AVAuthorizationStatus.Authorized {
    // アクセス許可あり
} else if status == AVAuthorizationStatus.Restricted {
    // ユーザー自身にカメラへのアクセスが許可されていない
} else if status == AVAuthorizationStatus.NotDetermined {
    // まだアクセス許可を聞いていない
} else if status == AVAuthorizationStatus.Denied {
    // アクセス許可されていない
}

カメラロール

AssetsLibraryを使います。

import AssetsLibrary

let status = ALAssetsLibrary.authorizationStatus()
if status == ALAuthorizationStatus.Authorized {
    // アクセス許可あり
} else if status == ALAuthorizationStatus.Restricted {
    // ユーザー自身にカメラへのアクセスが許可されていない
} else if status == ALAuthorizationStatus.NotDetermined {
    // まだアクセス許可を聞いていない
} else if status == ALAuthorizationStatus.Denied {
    // アクセス許可されていない
}

Push通知

これは iOS7iOS8 で対応方法が変わります。

// iOS7の場合
let remoteNotificationType: UIRemoteNotificationType = UIApplication.sharedApplication().enabledRemoteNotificationTypes()

// Noneじゃなかったら許可あり
let isAuthorization = (remoteNotificationType != UIRemoteNotificationType.None)

// iOS8の場合
UIApplication.sharedApplication().isRegisteredForRemoteNotifications()

となります。

ちなみにUIRemoteNotificationTypeは以下のような

struct UIRemoteNotificationType : RawOptionSetType {
    init(_ rawValue: UInt)
    init(rawValue rawValue: UInt)
    static var None: UIRemoteNotificationType { get }
    static var Badge: UIRemoteNotificationType { get }
    static var Sound: UIRemoteNotificationType { get }
    static var Alert: UIRemoteNotificationType { get }
    static var NewsstandContentAvailability: UIRemoteNotificationType { get }
}

という構造体になります。

以上です。

100
92
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
100
92