Skip to content

Instantly share code, notes, and snippets.

@SongJiaqiang
Created November 2, 2016 04:11
Show Gist options
  • Save SongJiaqiang/d9284f2dbffdc85b6fdb4a7a1c7ff02f to your computer and use it in GitHub Desktop.
Save SongJiaqiang/d9284f2dbffdc85b6fdb4a7a1c7ff02f to your computer and use it in GitHub Desktop.
iOS9 之后需要用户手动授予app权限,这段代码简化了请求权限的代码书写。暂时支持相机、相册、麦克风,其它权限以后再加。
//
// AuthorizationTool.h
// Rokk
//
// Created by Jarvis on 01/11/2016.
// Copyright © 2016 JQTech. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSUInteger, AuthorizationStatus) {
AuthorizationStatusAuthorized = 0, // 已授权
AuthorizationStatusDenied, // 拒绝
AuthorizationStatusRestricted, // 应用没有相关权限,且当前用户无法改变这个权限,比如:家长控制
AuthorizationStatusNotSupport // 硬件等不支持
};
@interface AuthorizationTool : NSObject
/**
* 请求所有权限(相机、相册、麦克风)
*/
+ (void)requestAllAuthorization;
/**
* 请求相册访问权限
*
* @param callback <#callback description#>
*/
+ (void)requestImagePickerAuthorization:(void(^)(AuthorizationStatus status))callback;
/**
* 请求相机权限
*
* @param callback <#callback description#>
*/
+ (void)requestCameraAuthorization:(void(^)(AuthorizationStatus status))callback;
/**
* 请求麦克风权限
*
* @param callback <#callback description#>
*/
+ (void)requestMicrophoneAuthorization:(void(^)(AuthorizationStatus status))callback;
//+ (void)requestAddressBookAuthorization:(void (^)(AuthorizationStatus))callback;
@end
//
// AuthorizationTool.m
// Rokk
//
// Created by Jarvis on 01/11/2016.
// Copyright © 2016 JQTech. All rights reserved.
//
#import "AuthorizationTool.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <Photos/Photos.h>
@implementation AuthorizationTool
+ (void)requestAllAuthorization {
[AuthorizationTool requestCameraAuthorization:^(AuthorizationStatus status) {
[AuthorizationTool responseAuthorizationStatus:status permissionName:@"Camera"];
}];
[AuthorizationTool requestMicrophoneAuthorization:^(AuthorizationStatus status) {
[AuthorizationTool responseAuthorizationStatus:status permissionName:@"Audio Record"];
}];
[AuthorizationTool requestImagePickerAuthorization:^(AuthorizationStatus status) {
[AuthorizationTool responseAuthorizationStatus:status permissionName:@"Album"];
}];
}
+ (void)responseAuthorizationStatus:(AuthorizationStatus)status permissionName:(NSString *)name {
switch (status) {
case AuthorizationStatusAuthorized:
{
NSLog(@"Authorized");
break;
}
case AuthorizationStatusDenied:
case AuthorizationStatusRestricted:
{
UIViewController *rootController = [UIApplication sharedApplication].keyWindow.rootViewController;
NSString *title = [NSString stringWithFormat:@"Please Grant %@ Permission to Rokk in The Settings App", name];
UIAlertController *authAlert = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
[authAlert addAction: [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:nil]];
[authAlert addAction: [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSURL *settingUrl = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:settingUrl]) {
[[UIApplication sharedApplication] openURL:settingUrl];
}
}]];
[rootController presentViewController:authAlert animated:YES completion:nil];
break;
}
case AuthorizationStatusNotSupport:
{
NSLog(@"Device Not Support");
break;
}
default:
break;
}
}
#pragma mark - 相册
+ (void)requestImagePickerAuthorization:(void(^)(AuthorizationStatus status))callback {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ||
[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
ALAuthorizationStatus authStatus = [ALAssetsLibrary authorizationStatus];
if (authStatus == ALAuthorizationStatusNotDetermined) { // 未授权
if ([UIDevice currentDevice].systemVersion.floatValue < 8.0) {
[self executeCallback:callback status:AuthorizationStatusAuthorized];
} else {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
[self executeCallback:callback status:AuthorizationStatusAuthorized];
} else if (status == PHAuthorizationStatusDenied) {
[self executeCallback:callback status:AuthorizationStatusDenied];
} else if (status == PHAuthorizationStatusRestricted) {
[self executeCallback:callback status:AuthorizationStatusRestricted];
}
}];
}
} else if (authStatus == ALAuthorizationStatusAuthorized) {
[self executeCallback:callback status:AuthorizationStatusAuthorized];
} else if (authStatus == ALAuthorizationStatusDenied) {
[self executeCallback:callback status:AuthorizationStatusDenied];
} else if (authStatus == ALAuthorizationStatusRestricted) {
[self executeCallback:callback status:AuthorizationStatusRestricted];
}
} else {
[self executeCallback:callback status:AuthorizationStatusNotSupport];
}
}
#pragma mark - 相机
+ (void)requestCameraAuthorization:(void (^)(AuthorizationStatus))callback {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusNotDetermined) {
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
[self executeCallback:callback status:AuthorizationStatusAuthorized];
} else {
[self executeCallback:callback status:AuthorizationStatusDenied];
}
}];
} else if (authStatus == AVAuthorizationStatusAuthorized) {
[self executeCallback:callback status:AuthorizationStatusAuthorized];
} else if (authStatus == AVAuthorizationStatusDenied) {
[self executeCallback:callback status:AuthorizationStatusDenied];
} else if (authStatus == AVAuthorizationStatusRestricted) {
[self executeCallback:callback status:AuthorizationStatusRestricted];
}
} else {
[self executeCallback:callback status:AuthorizationStatusNotSupport];
}
}
+ (void)requestMicrophoneAuthorization:(void (^)(AuthorizationStatus))callback {
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
AVAudioSessionRecordPermission recordPermission = [audioSession recordPermission];
switch (recordPermission) {
case AVAudioSessionRecordPermissionUndetermined:
{
[audioSession requestRecordPermission:^(BOOL granted) {
if (granted) {
[self executeCallback:callback status:AuthorizationStatusAuthorized];
} else {
[self executeCallback:callback status:AuthorizationStatusDenied];
}
}];
break;
}
case AVAudioSessionRecordPermissionGranted:
[self executeCallback:callback status:AuthorizationStatusAuthorized];
break;
case AVAudioSessionRecordPermissionDenied:
[self executeCallback:callback status:AuthorizationStatusDenied];
break;
default:
[self executeCallback:callback status:AuthorizationStatusNotSupport];
break;
}
}
#pragma mark - callback
+ (void)executeCallback:(void (^)(AuthorizationStatus))callback status:(AuthorizationStatus)status {
dispatch_async(dispatch_get_main_queue(), ^{
if (callback) {
callback(status);
}
});
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment