实用小技巧(二十六)—— APP中各种权限监测和申请工具封装
版本记录
版本号 | 时间 |
---|---|
V1.0 | 2017.11.01 |
前言
在app中,很多时候都需要熟悉一些小技巧让您事半功倍,提高工作效率,这些小技巧都很小很细。感兴趣的可以看看我写的其他小技巧。
1. 实用小技巧(一):UIScrollView中上下左右滚动方向的判断
2. 实用小技巧(二):屏幕横竖屏的判断和相关逻辑
3.实用小技巧(三):点击手势屏蔽子视图的响应
4.实用小技巧(四):动态的增删标签视图
5.实用小技巧(五):通过相册或者相机更改图标
6.实用小技巧(六):打印ios里所有字体
7. 实用小技巧(七):UITableViewCell自适应行高的计算
8. 实用小技巧(八):数字余额显示的分隔
9.实用小技巧(九):类头条模糊背景的实现
10.实用小技巧(十):晃动手机换后台服务器网络
11.实用小技巧(十一):scrollView及其子类显示的一些异常处理
12.实用小技巧(十二):头像图片缩放以及保存到相册简单功能的实现
13.实用小技巧(十三):一种类酷我音乐盒动画实现
14.实用小技巧(十四):生成跳往applestore指定app的方法
15.实用小技巧(十五):左侧向右滑动返回上一级控制器
16.实用小技巧(十六):获取设备信息
17.实用小技巧(十七):清除缓存目录
18.实用小技巧(十八):取出gif图的每一帧
19.实用小技巧(十九):获取相机和麦克风权限
20.实用小技巧(二十):游客模式的实现
21.实用小技巧(二十一):版本检测的实现
22.实用小技巧(二十二):键盘遮挡问题
23. 实用小技巧(二十三):获取闪光灯的控制
24. 实用小技巧(二十四):APP页面开启跳转逻辑处理
25. 实用小技巧(二十五)—— 关于UILabel控件宽高的计算
权限监测请求工具的封装
下面我们封装一下权限监测和请求工具,直接看代码吧。
1. JJAuthorityManager.h
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSUInteger, JJAuthorizationType) {
JJAuthorizationTypeNotDetermined, //未决定
JJAuthorizationTypeDenied, //拒绝
JJAuthorizationTypeAllow //允许
};
/**
权限获取及请求
*/
@interface JJAuthorityManager : NSObject
/**
检查权限
*/
//检查网络权限
+ (JJAuthorizationType)jj_checkNetworkAuthorization;
//检查相机权限
+ (JJAuthorizationType)jj_checkCameraAuthorization;
//检查相册权限
+ (JJAuthorizationType)jj_checkAlbumAuthorization;
//检查麦克风权限
+ (JJAuthorizationType)jj_checkMicrophoneAuthorization;
//检查定位权限
+ (JJAuthorizationType)jj_checkLocationAuthorization;
//检查通知权限
+ (void)jj_checkNotificationAuthorized:(void (^)(BOOL authorized))completion;
/**
请求权限
*/
//打开设置授权视图
+ (void)jj_openAuthorizationView;
//获取相机权限
+ (void)jj_getCameraAuthorization:(void (^)(BOOL granted))completion;
//获取相册权限
+ (void)jj_getAlbumAuthorization:(void (^)(BOOL authorized))completion;
//获取麦克风权限
+ (void)jj_getMicrophoneAuthorization:(void (^)(BOOL granted))completion;
//获取通知权限
+ (void)jj_getNotificationAuthorization;
@end
2. JJAuthorityManager.m
#import "JJAuthorityManager.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreLocation/CoreLocation.h>
#import <CoreTelephony/CTCellularData.h>
#import <Photos/Photos.h>
#import <UserNotifications/UserNotifications.h>
@implementation JJAuthorityManager
//检查网络权限
+ (JJAuthorizationType)jj_checkNetworkAuthorization
{
CTCellularData *cellularData = [[CTCellularData alloc] init];
CTCellularDataRestrictedState state = cellularData.restrictedState;
if (state == kCTCellularDataRestricted) {
return JJAuthorizationTypeDenied;
}
else if (state == kCTCellularDataNotRestricted) {
return JJAuthorizationTypeAllow;
}
else if (state == kCTCellularDataRestrictedStateUnknown) {
return JJAuthorizationTypeNotDetermined;
}
return JJAuthorizationTypeAllow;
}
//检查相机权限
+ (JJAuthorizationType)jj_checkCameraAuthorization
{
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if(authStatus == ALAuthorizationStatusRestricted || authStatus == ALAuthorizationStatusDenied){
return JJAuthorizationTypeDenied;
}
else if (authStatus == ALAuthorizationStatusNotDetermined) {
return JJAuthorizationTypeNotDetermined;
}
return JJAuthorizationTypeAllow;
}
//检查相册权限
+ (JJAuthorizationType)jj_checkAlbumAuthorization
{
PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus];
if (authStatus == PHAuthorizationStatusAuthorized) {
return JJAuthorizationTypeAllow;
}
else if (authStatus == PHAuthorizationStatusDenied) {
return JJAuthorizationTypeDenied;
}
else if (authStatus == PHAuthorizationStatusNotDetermined) {
return JJAuthorizationTypeNotDetermined;
}
else if (authStatus == PHAuthorizationStatusRestricted) {
return JJAuthorizationTypeDenied;
}
return JJAuthorizationTypeAllow;
}
//检查麦克风权限
+ (JJAuthorizationType)jj_checkMicrophoneAuthorization
{
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];//麦克风权限
if (authStatus == AVAuthorizationStatusAuthorized) {
return JJAuthorizationTypeAllow;
}
else if (authStatus == AVAuthorizationStatusDenied) {
return JJAuthorizationTypeDenied;
}
else if (authStatus == AVAuthorizationStatusNotDetermined) {
return JJAuthorizationTypeNotDetermined;
}
else if (authStatus == AVAuthorizationStatusRestricted) {
return JJAuthorizationTypeDenied;
}
return JJAuthorizationTypeAllow;
}
//检查定位权限
+ (JJAuthorizationType)jj_checkLocationAuthorization
{
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (kCLAuthorizationStatusDenied == status || kCLAuthorizationStatusRestricted == status) {
return JJAuthorizationTypeDenied;
} else if (kCLAuthorizationStatusNotDetermined == status) {
return JJAuthorizationTypeNotDetermined;
}
return JJAuthorizationTypeAllow;
}
//检查通知权限
+ (void)jj_checkNotificationAuthorized:(void (^)(BOOL authorized))completion
{
if (!completion) {
return;
}
if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0) {
[[UNUserNotificationCenter currentNotificationCenter]
getNotificationSettingsWithCompletionHandler:
^(UNNotificationSettings * _Nonnull settings) {
completion(settings.authorizationStatus == UNAuthorizationStatusAuthorized);
}];
}
else {
completion([UIApplication sharedApplication].currentUserNotificationSettings.types != UIUserNotificationTypeNone);
}
}
//打开设置授权视图
+ (void)jj_openAuthorizationView
{
NSURL *settingURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:settingURL]) {
[[UIApplication sharedApplication] openURL:settingURL];
}
}
//获取相机权限
+ (void)jj_getCameraAuthorization:(void (^)(BOOL granted))completion
{
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted){
NSLog(@"用户同意授权相机");
}
else {
NSLog(@"用户拒绝授权相机");
}
if (completion) {
completion(granted);
}
}];
}
//获取相册权限
+ (void)jj_getAlbumAuthorization:(void (^)(BOOL authorized))completion {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
NSLog(@"用户同意授权相册");
}
else {
NSLog(@"用户拒绝授权相册");
}
if (completion) {
completion(status == PHAuthorizationStatusAuthorized);
}
}];
}
// 获取麦克风权限
+ (void)jj_getMicrophoneAuthorization:(void (^)(BOOL granted))completion
{
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
if (granted) {
NSLog(@"用户同意授权麦克风");
}
else{
NSLog(@"用户拒绝授权麦克风");
}
if (completion) {
completion(granted);
}
}];
}
//获取通知权限
+ (void)jj_getNotificationAuthorization
{
if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request authorization succeeded!");
}
}];
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"%@",settings);
}];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}
@end
工具的使用
下面我们就看一下该工具的使用情况,看一下测试代码。
1. ViewController.m
#import "ViewController.h"
#import "JJAuthorityManager.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
//检查请求通知权限
[JJAuthorityManager jj_checkNotificationAuthorized:^(BOOL authorized) {
if (authorized) {
NSLog(@"已经授权了");
}
else {
[JJAuthorityManager jj_getNotificationAuthorization];
}
}];
sleep(3);
//检查相册权限
JJAuthorizationType type = [JJAuthorityManager jj_checkAlbumAuthorization];
if (type == JJAuthorizationTypeAllow) {
NSLog(@"相册已经授权");
}
else{
[JJAuthorityManager jj_getAlbumAuthorization:^(BOOL authorized) {
if (authorized) {
NSLog(@"相册获取授权成功");
}
else{
NSLog(@"相册获取授权失败");
}
}];
}
}
@end
下面我们看一下效果图。
可见都是先获取权限,没有请求权限弹出的这个弹窗,这里请求相册权限别忘记配置plist权限啊,否则会崩溃的。具体代码可以见刀客传奇 - Github
后记
未完,待续~~~