1. 麦克风权限

单存检测是否有麦克风权限,并不会弹出是否允许弹出权限提示框

#import

/**

判断当前是有语音权限,但是不会弹出是否允许弹出权限

(需要在info中配置)Privacy – Microphone Usage Description 允许**访问您的语音,来用于**功能?

@return YES:有权限,NO:没权限

*/

– (BOOL)JX_Device_Permission_AudioAuth {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];

if (authStatus == AVAuthorizationStatusDenied || authStatus == AVAuthorizationStatusRestricted) {
return NO;

}

return YES;

}复制代码

检测是否有权限,如果没有授权过,会弹出是否允许提示框

#import

/**

判断当前是有语音权限,会弹出是否允许弹出权限

(需要在info中配置)Privacy – Microphone Usage Description 允许**访问您的语音,来用于**功能?

*/

– (void)JX_Device_Permission_Check_AudioAuth {
AVAudioSession *session = [AVAudioSession sharedInstance];

if ([session respondsToSelector:@selector(requestRecordPermission:)]){
[session performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
// do something

}];

}

}复制代码

2. 访问相册权限

检测是否有访问相册权限,并不会弹出是否允许访问相册权限提示框

#import

#import

/**

判断相册权限开关,但是不会弹出是否允许弹出权限

(需要在info中配置)Privacy – Photo Library Additions Usage Description 允许**访问您的相册,来用于**功能

@return YES:有权限,NO:没权限

*/

– (BOOL)JX_Device_Permission_PhotoLibraryAuth {
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus];

if(authStatus == PHAuthorizationStatusDenied || authStatus == PHAuthorizationStatusRestricted) {
return NO;

}

} else if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0 && [[UIDevice currentDevice].systemVersion floatValue] < 8.0) {
ALAuthorizationStatus authStatus = [ALAssetsLibrary authorizationStatus];

if(authStatus == ALAuthorizationStatusDenied || authStatus == ALAuthorizationStatusRestricted) {
return NO;

}

}

return YES;

}复制代码

检测是否有权限,如果没有授权过,会弹出是否允许提示框

#import

/**

判断相册权限开关,会弹出是否允许弹出权限

(需要在info中配置)Privacy – Photo Library Additions Usage Description 允许**访问您的相册,来用于**功能

*/

– (void)JX_Device_Permission_Check_PhotoLibraryAuth{
BOOL auth = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];

if (!auth) return;

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { //弹出访问权限提示框

if (status == PHAuthorizationStatusAuthorized) { // 有权限

dispatch_async(dispatch_get_main_queue(),^{
// do something

// 一般操作

self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

[self presentViewController:self.imagePickerController animated:YES completion:nil];

});

} else {
dispatch_async(dispatch_get_main_queue(),^{ // 无权限

// do something

});

}

}];

}复制代码

3. 访问相机权限

检测是否有相机拍照权限,并不会弹出是否允许提示框

#import

/**

判断相机权限开关,但是不会弹出是否允许弹出权限

(需要在info中配置)Privacy – Camera Usage Description 允许**访问您的相机,来用于**功能

@return YES:有权限,NO:没权限

*/

– (BOOL)JX_Device_Permission_CameraAuth {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

if (authStatus == AVAuthorizationStatusDenied || authStatus == AVAuthorizationStatusRestricted) {
return NO;

}

return YES;

}复制代码

检测是否有过授权,如果没有授权过,会弹出是否允许提示框

#import

/**

判断相机权限开关,会弹出是否允许弹出权限

(需要在info中配置)Privacy – Camera Usage Description 允许**访问您的相机,来用于**功能

*/

– (void)JX_Device_Permission_Check_CameraAuth {
BOOL auth = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];

if (!auth) permission(NO);

NSString *mediaType = AVMediaTypeVideo;//读取媒体类型

[AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
dispatch_async(dispatch_get_main_queue(),^{
if (granted) { // 授权成功

// do something

// 一般会做的操作,跳转到系统的相机

self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

[self presentViewController:self.imagePickerController animated:YES completion:nil];

} else { // 拒*授权

// do something

}

});

}];

}复制代码

4. 推送权限(远程、本地)

检测是否有推送权限,并不会弹出是否允许提示框

/**

推送权限开关

@return YES:有权限,NO:没权限

*/

– (BOOL)JX_Device_Permission_NotificationAuth {
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0f) {
UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];

if (UIUserNotificationTypeNone == setting.types) {
return NO;

}

} else {
UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

if(UIRemoteNotificationTypeNone == type){
return NO;

}

}

return YES;

}复制代码

检测是否有过授权,如果没有授权过,会弹出是否允许提示框

说明:这里仅仅是检测是否授权过,弹出提示框操作。至于远程注册一些流程请参见另一篇博客。iOS 通知权限(远程通知、本地通知)

#ifdef NSFoundationVersionNumber_iOS_9_x_Max

#import

#endif

/**

判断通知权限开关,会弹出是否允许弹出权限(远程、本地)

*/

– (void)JX_Device_Permission_Check_NotificationAuth {
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
// do something

// 对granted 进行判断,是否允许权限

});

}];

} else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:setting];

BOOL auth = [self JX_Device_Permission_NotificationAuth];

// 对auth 进行判断,是否允许权限

}

}复制代码