iOS各种访问权限总结

作者: Show_Perry | 来源:发表于2017-03-31 17:02 被阅读2262次

    麦克风访问(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

    • 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中添加对应的权限请求说明。如下所示

    • 使用照相(Camera)功能:Privacy - Camera Usage Description
    • 使用HealthKit读数据: Privacy - Health Share Usage Description
    • 使用HealthKit写数据: Privacy - Health Update Usage Description
    • 定位功能: Privacy - Location Always Usage Description Privacy - Location When In Use Usage Description
    • 使用麦克风(Microphone):Privacy - Microphone Usage Description
    • 使用相册功能: Privacy - Photo Library Usage Description
    • BLE 设备作为外设 :Privacy - Bluetooth Peripheral Usage Description
    • 通讯录访问:Privacy - Contacts Usage Description
    • 日历事件访问: Privacy - Calendars Usage Description
    • 媒体库访问: Privacy - Media Library Usage Description

    如果还有未补充的请留言

    相关文章

      网友评论

      • 我的时代我开创:为什么虚拟机上[sharedSession requestRecordPermission:^(BOOL granted) 不生效啊?
      • 61d817c65aa8:为啥我联网设置权限了。但是iOS app不提示 直接自动就有网了
        没有网络授权提示框
        Show_Perry:@殇夏_75f9 这个是没有的!
        61d817c65aa8:@Show_Perry 对啊。 但是没有提示框呀
        Show_Perry:你说的是那个联网设置?这个:App Transport Security Settings?
      • wlqsmiling:用这个MPMediaPickerController打开音乐列表的权限怎么查看有没有?
        Show_Perry:感谢提醒,已更新。

      本文标题:iOS各种访问权限总结

      本文链接:https://www.haomeiwen.com/subject/jhmbgttx.html