最近在開發(fā)過程中植榕,遇到了權(quán)限問題≡俨祝現(xiàn)整理一下供大家參考。
首先碰到權(quán)限問題尊残,要在info.plist中配置炒瘸,如下圖:
1、相冊權(quán)限校驗(yàn)寝衫,代碼如下:
/**
校驗(yàn)相冊權(quán)限
*/
+ (void)checkPhotoAuth:(void (^)(BOOL auth))result {
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0 //iOS 8.0以下使用AssetsLibrary.framework
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
switch (status) {
case ALAuthorizationStatusNotDetermined: //用戶還沒有選擇(第一次)
{
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (*stop) {
//允許
if (result) {
dispatch_async(dispatch_get_main_queue(), ^{
result(YES);
});
}
return;
}
*stop = YES;
} failureBlock:^(NSError *error) {
//拒絕
if (result) {
dispatch_async(dispatch_get_main_queue(), ^{
result(NO);
});
}
}];
}
break;
case ALAuthorizationStatusRestricted: //家長控制
{
if (result) {
result(NO);
}
}
break;
case ALAuthorizationStatusDenied: //用戶拒絕
{
if (result) {
result(NO);
}
}
break;
case ALAuthorizationStatusAuthorized: //已授權(quán)
{
if (result) {
result(YES);
}
}
break;
default:
break;
}
#else //iOS 8.0以上使用Photos.framework
PHAuthorizationStatus current = [PHPhotoLibrary authorizationStatus];
switch (current) {
case PHAuthorizationStatusNotDetermined: //用戶還沒有選擇(第一次)
{
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
//授權(quán)
if (result) {
dispatch_async(dispatch_get_main_queue(), ^{
result(YES);
});
}
}else {
//其他
if (result) {
dispatch_async(dispatch_get_main_queue(), ^{
result(NO);
});
}
}
}];
}
break;
case PHAuthorizationStatusRestricted: //家長控制
{
if (result) {
result(NO);
}
}
break;
case PHAuthorizationStatusDenied: //用戶拒絕
{
if (result) {
result(NO);
}
}
break;
case PHAuthorizationStatusAuthorized: //已授權(quán)
{
if (result) {
result(YES);
}
}
break;
default:
break;
}
#endif
}
2顷扩、相機(jī)權(quán)限校驗(yàn),代碼如下:
/**
校驗(yàn)相機(jī)權(quán)限
*/
+ (void)checkVideoAuth:(void (^)(BOOL auth))result {
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
switch (status) {
case AVAuthorizationStatusNotDetermined: //用戶還沒有選擇(第一次)
{
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) { //授權(quán)
dispatch_async(dispatch_get_main_queue(), ^{
if (result) {
result(YES);
}
});
}else { //拒絕
dispatch_async(dispatch_get_main_queue(), ^{
if (result) {
result(NO);
}
});
}
}];
}
break;
case AVAuthorizationStatusRestricted: //家長控制
{
if (result) {
result(NO);
}
}
break;
case AVAuthorizationStatusDenied: //用戶拒絕
{
if (result) {
result(NO);
}
}
break;
case AVAuthorizationStatusAuthorized: //已授權(quán)
{
if (result) {
result(YES);
}
}
break;
default:
break;
}
}
3慰毅、麥克風(fēng)權(quán)限校驗(yàn)隘截,代碼如下:
/**
校驗(yàn)錄音權(quán)限
*/
+ (void)checkAudioAuth:(void (^)(BOOL auth))result {
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
switch (status) {
case AVAuthorizationStatusNotDetermined: //用戶還沒有選擇(第一次)
{
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
if (granted) { //授權(quán)
dispatch_async(dispatch_get_main_queue(), ^{
if (result) {
result(YES);
}
});
}else { //拒絕
dispatch_async(dispatch_get_main_queue(), ^{
if (result) {
result(NO);
}
});
}
}];
}
break;
case AVAuthorizationStatusRestricted: //家長控制
{
if (result) {
result(NO);
}
}
break;
case AVAuthorizationStatusDenied: //用戶拒絕
{
if (result) {
result(NO);
}
}
break;
case AVAuthorizationStatusAuthorized: //已授權(quán)
{
if (result) {
result(YES);
}
}
break;
default:
break;
}
}
4、通訊錄權(quán)限校驗(yàn)汹胃,代碼如下:
/**
校驗(yàn)通訊錄權(quán)限
*/
+ (void)checkAddressBookAuth:(void (^)(BOOL auth))result {
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
switch (status) {
case kABAuthorizationStatusNotDetermined: //用戶還沒有選擇(第一次)
{
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (granted) { //授權(quán)
dispatch_async(dispatch_get_main_queue(), ^{
if (result) {
result(YES);
}
});
}else { //拒絕
dispatch_async(dispatch_get_main_queue(), ^{
if (result) {
result(NO);
}
});
}
});
}
break;
case kABAuthorizationStatusRestricted: //家長控制
{
if (result) {
result(NO);
}
}
break;
case kABAuthorizationStatusDenied: //用戶拒絕
{
if (result) {
result(NO);
}
}
break;
case kABAuthorizationStatusAuthorized: //已授權(quán)
{
if (result) {
result(YES);
}
}
break;
default:
break;
}
#else
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
switch (status) {
case CNAuthorizationStatusNotDetermined: //用戶還沒有選擇(第一次)
{
CNContactStore *contactStore = [[CNContactStore alloc] init];
[contactStore requestAccessForEntityType:CNEntityTypeContacts
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) { //授權(quán)
dispatch_async(dispatch_get_main_queue(), ^{
if (result) {
result(YES);
}
});
}else { //拒絕
dispatch_async(dispatch_get_main_queue(), ^{
if (result) {
result(NO);
}
});
}
}];
}
break;
case CNAuthorizationStatusRestricted: //家長控制
{
if (result) {
result(NO);
}
}
break;
case CNAuthorizationStatusDenied: //用戶拒絕
{
if (result) {
result(NO);
}
}
break;
case CNAuthorizationStatusAuthorized: //已授權(quán)
{
if (result) {
result(YES);
}
}
break;
default:
break;
}
#endif
}
好了婶芭,到此結(jié)束。