Skip to content

权限请求 Permission

Damon edited this page Apr 10, 2018 · 1 revision

在HDCommonTools+Permission文件中,是去获取各类权限的函数功能。

In the HDCommonTools + Permission file, it's a function function to get various permissions.

添加权限说明 Add permission description

获取各类权限之前,需要在工程对应的info.plist文件中声明权限的使用目的。

Obtain all kinds of permissions before, you need to project corresponding info.plist file to declare the purpose of the use of permissions.

such as

<key>NSPhotoLibraryUsageDescription</key>
	<string>是否允许访问您的相册以便获取保存你的截图?</string>

权限的通知 Permission notice

每个获取权限时,在用户做出选择后,都会有统一的通知信息

Each access to permission, the user to make a choice, there will be a unified notice of information

///系统权限授权变化通知
//System authority authorization change notification
FOUNDATION_EXPORT NSString * const HDPermissionStatusDidChangeNotification;

///系统变化通知中的userinfo的key,标记名字
//The key of the userinfo in the system change notification, marked name
FOUNDATION_EXPORT NSString * const HDPermissionNameItem;

///系统变化通知中的userinfo的key,标记状态
//The key of userinfo in the system change notification, marked state
FOUNDATION_EXPORT NSString * const HDPermissionStatusItem;

其中HDPermissionNameItem 对应的是HDPrivatePermissionName

HDPermissionPermissionName corresponds to HDPrivatePermissionName

///申请的权限 Application authority
typedef NS_ENUM(NSUInteger, HDPrivatePermissionName) {
    kHDPermissionNameAudio,     //麦克风权限 Microphone permissions
    kHDPermissionNameVideo,     //摄像头权限 Camera permissions
    kHDPermissionNamePhotoLib,  //相册权限 Photo album permissions
    kHDPermissionNameGPS,       //GPS权限 GPS permissions
};

HDPermissionStatusItem 对应的是HDPrivatePermissionStatus

HDPermissionStatusItem corresponds to HDPrivatePermissionStatus

///申请权限状态 Application permissions status
typedef NS_ENUM(NSUInteger, HDPrivatePermissionStatus) {
    kHDAuthorized = 1,  //用户允许 Authorized
    kHDAuthorRestricted,//被限制修改不了状态,比如家长控制选项等 Restricted status, such as parental control options, etc.
    kHDDenied,          //用户拒绝 Denied
    kHDNotDetermined    //用户尚未选择 NotDetermined
};

使用示例 Use example

可以在vc界面创建时去注册通知

Can be registered in the vc interface to create a notification

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(permissionNotifacation:) name:HDPermissionStatusDidChangeNotification object:nil];

通过HDPermissionNameItem去判断对应的权限是否获取到

By HDPermissionNameItem to determine whether the corresponding permission is obtained

-(void)permissionNotifacation:(NSNotification*)notification{
    NSDictionary * userInfo = [notification userInfo];
    if ([[userInfo objectForKey:HDPermissionNameItem] integerValue] == kHDPermissionNameGPS) {
        if ([[userInfo objectForKey:HDPermissionStatusItem] integerValue] == kHDAuthorized) {
            NSLog(@"用户允许访问gps Users are allowed access to GPS");
        }else if ([[userInfo objectForKey:HDPermissionStatusItem] integerValue] == kHDAuthorRestricted){
            NSLog(@"用户被限制访问gps Users are restricted to access to GPS");
        }else if ([[userInfo objectForKey:HDPermissionStatusItem] integerValue] == kHDNotDetermined){
            NSLog(@"用户尚未选择是否允许访问gps User has not chosen to allow access to the GPS");
        }else{
            NSLog(@"用户不允许访问gps Users do not allow access to GPS");
        }
    }else if ([[userInfo objectForKey:HDPermissionNameItem] integerValue] == kHDPermissionNamePhotoLib){
        if ([[userInfo objectForKey:HDPermissionStatusItem] integerValue] == kHDAuthorized) {
            NSLog(@"用户允许访问相册 Users are allowed access to Album");
        }else if ([[userInfo objectForKey:HDPermissionStatusItem] integerValue] == kHDAuthorRestricted){
            NSLog(@"用户被限制访问相册 Users are restricted to access to Album");
        }else if ([[userInfo objectForKey:HDPermissionStatusItem] integerValue] == kHDNotDetermined){
            NSLog(@"用户尚未选择是否允许访问相册 User has not chosen to allow access to the Album");
        }else{
            NSLog(@"用户不允许访问相册 Users do not allow access to Album");
        }
    }
}