移动端设计研发IOS文件iOS Developer

iOS各种访问权限总结

2017-03-31  本文已影响2262人  Show_Perry

麦克风访问(AVAudioSession)

- (AVAudioSessionRecordPermission)recordPermission NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED __WATCHOS_PROHIBITED;

typedef NS_OPTIONS(NSUInteger, AVAudioSessionRecordPermission)
{
    AVAudioSessionRecordPermissionUndetermined      = 'undt', //未定,即还没请求权限
    AVAudioSessionRecordPermissionDenied            = 'deny', //拒绝
    AVAudioSessionRecordPermissionGranted           = 'grnt' //同意
} NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED __WATCHOS_PROHIBITED;


- (void)requestRecordPermission:(PermissionBlock)response NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED __WATCHOS_PROHIBITED;

[[AVAudioSession sharedInstance]requestRecordPermission:^(BOOL granted){
}];

相机访问(AVCaptureDevice)

+ (AVAuthorizationStatus)authorizationStatusForMediaType:(NSString *)mediaType NS_AVAILABLE_IOS(7_0);


typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {
    AVAuthorizationStatusNotDetermined = 0, //未决定,即还没请求权限
    AVAuthorizationStatusRestricted, //不允许访问
    AVAuthorizationStatusDenied, //拒绝
    AVAuthorizationStatusAuthorized //允许
} NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;


+ (void)requestAccessForMediaType:(NSString *)mediaType completionHandler:(void (^)(BOOL granted))handler NS_AVAILABLE_IOS(7_0);

mediaType的类型有:AVMediaTypeVideo,AVMediaTypeAudio.

相册访问

iOS 6.0-9.0(ALAssetsLibrary)

#import <AssetsLibrary/AssetsLibrary.h>

+ (ALAuthorizationStatus)authorizationStatus NS_DEPRECATED_IOS(6_0, 9_0, "Use authorizationStatus on the shared PHPhotoLibrary from the Photos framework instead");

typedef NS_ENUM(NSInteger, ALAuthorizationStatus) {
    ALAuthorizationStatusNotDetermined NS_ENUM_DEPRECATED_IOS(6_0, 9_0) = 0, // User has not yet made a choice with regards to this application
    ALAuthorizationStatusRestricted NS_ENUM_DEPRECATED_IOS(6_0, 9_0),        // This application is not authorized to access photo data.
                                            // The user cannot change this application’s status, possibly due to active restrictions
                                            //  such as parental controls being in place.
    ALAuthorizationStatusDenied NS_ENUM_DEPRECATED_IOS(6_0, 9_0),            // User has explicitly denied this application access to photos data.
    ALAuthorizationStatusAuthorized NS_ENUM_DEPRECATED_IOS(6_0, 9_0)        // User has authorized this application to access photos data.
} NS_DEPRECATED_IOS(6_0, 9_0, "Use PHAuthorizationStatus in the Photos framework instead");

iOS 8.0+ (PHPhotoLibrary)

#import <Photos/Photos.h>
+ (PHAuthorizationStatus)authorizationStatus;

typedef NS_ENUM(NSInteger, PHAuthorizationStatus) {
    PHAuthorizationStatusNotDetermined = 0, // User has not yet made a choice with regards to this application
    PHAuthorizationStatusRestricted,        // This application is not authorized to access photo data.
                                            // The user cannot change this application’s status, possibly due to active restrictions
                                            //   such as parental controls being in place.
    PHAuthorizationStatusDenied,            // User has explicitly denied this application access to photos data.
    PHAuthorizationStatusAuthorized         // User has authorized this application to access photos data.
} PHOTOS_AVAILABLE_IOS_TVOS(8_0, 10_0);


+ (void)requestAuthorization:(void(^)(PHAuthorizationStatus status))handler;

相册相机访问(读取-UIImagePickerController)

+ (BOOL)isSourceTypeAvailable:(UIImagePickerControllerSourceType)sourceType;                 // returns YES if source is available (i.e. camera present)

typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
    UIImagePickerControllerSourceTypePhotoLibrary,
    UIImagePickerControllerSourceTypeCamera,
    UIImagePickerControllerSourceTypeSavedPhotosAlbum
} __TVOS_PROHIBITED;


定位

#import <CoreLocation/CoreLocation.h>
+ (CLAuthorizationStatus)authorizationStatus __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_2);

typedef NS_ENUM(int, CLAuthorizationStatus) {
    // User has not yet made a choice with regards to this application
    kCLAuthorizationStatusNotDetermined = 0,

    // This application is not authorized to use location services.  Due
    // to active restrictions on location services, the user cannot change
    // this status, and may not have personally denied authorization
    kCLAuthorizationStatusRestricted,

    // User has explicitly denied authorization for this application, or
    // location services are disabled in Settings.
    kCLAuthorizationStatusDenied,

    // User has granted authorization to use their location at any time,
    // including monitoring for regions, visits, or significant location changes.
    //
    // This value should be used on iOS, tvOS and watchOS.  It is available on
    // MacOS, but kCLAuthorizationStatusAuthorized is synonymous and preferred.
    kCLAuthorizationStatusAuthorizedAlways NS_ENUM_AVAILABLE(10_12, 8_0),

    // User has granted authorization to use their location only when your app
    // is visible to them (it will be made visible to them if you continue to
    // receive location updates while in the background).  Authorization to use
    // launch APIs has not been granted.
    //
    // This value is not available on MacOS.  It should be used on iOS, tvOS and
    // watchOS.
    kCLAuthorizationStatusAuthorizedWhenInUse NS_ENUM_AVAILABLE(NA, 8_0),

    // User has authorized this application to use location services.
    //
    // This value is deprecated or prohibited on iOS, tvOS and watchOS.
    // It should be used on MacOS.
    kCLAuthorizationStatusAuthorized NS_ENUM_DEPRECATED(10_6, NA, 2_0, 8_0, "Use kCLAuthorizationStatusAuthorizedAlways") __TVOS_PROHIBITED __WATCHOS_PROHIBITED = kCLAuthorizationStatusAuthorizedAlways
};

+ (BOOL)locationServicesEnabled __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0);
- (void)requestWhenInUseAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);//应用使用时

- (void)requestAlwaysAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0) __TVOS_PROHIBITED;//一直打开

HealthKit

+ (BOOL)isHealthDataAvailable;

- (HKAuthorizationStatus)authorizationStatusForType:(HKObjectType *)type;

typedef NS_ENUM(NSInteger, HKAuthorizationStatus) {
    HKAuthorizationStatusNotDetermined = 0,
    HKAuthorizationStatusSharingDenied,
    HKAuthorizationStatusSharingAuthorized,
} HK_ENUM_AVAILABLE_IOS_WATCHOS(8_0, 2_0);



- (void)requestAuthorizationToShareTypes:(nullable NSSet<HKSampleType *> *)typesToShare
                               readTypes:(nullable NSSet<HKObjectType *> *)typesToRead
                              completion:(void (^)(BOOL success, NSError * _Nullable error))completion;
                              

通讯录

iOS9.0-

#import <AddressBook/AddressBook.h>

ABAuthorizationStatus authStatus = ABAddressBookGetAuthorizationStatus();
typedef CF_ENUM(CFIndex, ABAuthorizationStatus) {
    kABAuthorizationStatusNotDetermined = 0,    // deprecated, use CNAuthorizationStatusNotDetermined
    kABAuthorizationStatusRestricted,           // deprecated, use CNAuthorizationStatusRestricted
    kABAuthorizationStatusDenied,               // deprecated, use CNAuthorizationStatusDenied
    kABAuthorizationStatusAuthorized            // deprecated, use CNAuthorizationStatusAuthorized
} 


ABAddressBookRef addBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRequestAccessWithCompletion(addBook, ^(bool granted, CFErrorRef error){
        
    });


iOS9.0+

#import <Contacts/Contacts.h>

typedef NS_ENUM(NSInteger, CNEntityType)
{
    /*! The user's contacts. */
    CNEntityTypeContacts
}  NS_ENUM_AVAILABLE(10_11, 9_0);

+ (CNAuthorizationStatus)authorizationStatusForEntityType:(CNEntityType)entityType;

typedef NS_ENUM(NSInteger, CNAuthorizationStatus)
{
    /*! The user has not yet made a choice regarding whether the application may access contact data. */
    CNAuthorizationStatusNotDetermined = 0,
    /*! The application is not authorized to access contact data.
     *  The user cannot change this application’s status, possibly due to active restrictions such as parental controls being in place. */
    CNAuthorizationStatusRestricted,
    /*! The user explicitly denied access to contact data for the application. */
    CNAuthorizationStatusDenied,
    /*! The application is authorized to access contact data. */
    CNAuthorizationStatusAuthorized
} NS_ENUM_AVAILABLE(10_11, 9_0);
 CNContactStore *store = [[CNContactStore alloc] init];
 [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
    }];

日历事件和提醒

#import <EventKit/EventKit.h>

+ (EKAuthorizationStatus)authorizationStatusForEntityType:(EKEntityType)entityType NS_AVAILABLE(10_9, 6_0);

typedef NS_ENUM(NSInteger, EKAuthorizationStatus) {
    EKAuthorizationStatusNotDetermined = 0,
    EKAuthorizationStatusRestricted,
    EKAuthorizationStatusDenied,
    EKAuthorizationStatusAuthorized,
} NS_AVAILABLE(10_9, 6_0);

- (void)requestAccessToEntityType:(EKEntityType)entityType completion:(EKEventStoreRequestAccessCompletionHandler)completion NS_AVAILABLE(10_9, 6_0);

媒体库访问

虽然没有查看是否开启权限的接口,但是还是需要在Info.plist中添加说明。

- (IBAction)chooseMediaItem:(id)sender {
    MPMediaPickerController *mpc = [[MPMediaPickerController alloc]initWithMediaTypes:MPMediaTypeMusic];
    mpc.delegate = self;
    mpc.prompt =@"Please select a music";
    mpc.allowsPickingMultipleItems= NO;
    [self presentViewController:mpc animated:YES completion:nil];

}

#pragma mark - MPMediaPickerController delegate

- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection{
    /*insert your code*/
    MPMediaItem *mediaItem = [[mediaItemCollection items] objectAtIndex:0];
    self.item = mediaItem;
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


这里特别注意权限请求时需要在Info.plist中添加对应的权限请求说明。如下所示

如果还有未补充的请留言

上一篇下一篇

猜你喜欢

热点阅读