你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發基礎 >> iOS 應用部分權限控制

iOS 應用部分權限控制

編輯:IOS開發基礎

gulls-seagull-wings-feather-56593-medium.jpeg

整理下iOS開發中常用的權限控制,只整理裡一些常用的並不全。

#import typedef void (^AuthorizedFinishBlock)();

@interface LYAuthorizedMaster : NSObject


#pragma mark - 攝像頭權限
+(BOOL)checkCameraAuthority;
+(void)cameraAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail;

#pragma mark - 麥克風權限
+(BOOL)checkAudioAuthority;
+(void)audioAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail;

#pragma mark - 相冊權限
+(BOOL)checkAlbumAuthority;
+(void)albumAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail;

#pragma mark - 推送通知權限
+(BOOL)checkPushNotificationAuthority;
+(void)pushNotificationAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail;

#pragma mark - 推送通知權限
+(BOOL)checkLocationAuthority;
+(void)locationAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail;

#pragma mark - 通訊錄權限
+(BOOL)checkAddressBookAuthority;
+(void)AddressBookAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail;


下面是.m文件

裡面引入了很多庫文件,也不是所用項目都會用到的,用不到的注掉就好。

#import "LYAuthorizedMaster.h"
#import        //攝像頭麥克風 必須
#import      //相冊權限
#import        //位置權限
#import          //通訊錄權限

#import "AppDelegate.h"

#define kAPPName [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"]

@implementation LYAuthorizedMaster

#pragma mark -
+(BOOL)checkAuthority:(AVAuthorizationStatus)_status{
    return (_status == AVAuthorizationStatusAuthorized) || (_status == AVAuthorizationStatusNotDetermined);
}
+(void)showAlertController:(AuthorizedFinishBlock)_block device:(NSString *)_device{
    UIAlertController *_alertC = [UIAlertController alertControllerWithTitle:@"沒有權限" message:[NSString stringWithFormat:@"請開啟‘%@’對 %@ 的使用權限",kAPPName,_device] preferredStyle:UIAlertControllerStyleAlert];
    [_alertC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [_alertC addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }]];
    [((AppDelegate *)[UIApplication sharedApplication].delegate).window.rootViewController presentViewController:_alertC animated:YES completion:_block];
}

#pragma mark - 攝像頭權限
+(BOOL)checkCameraAuthority{
    return [self checkAuthority:[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]];
}
+(void)cameraAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail;{
    if ([self checkCameraAuthority]) {
        if (_success) {
            _success();
        }
    }else{
        [self showAlertController:_fail device:@"相機"];
    }
}

#pragma mark - 麥克風權限
+(BOOL)checkAudioAuthority{
    return [self checkAuthority:[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio]];
}
+(void)audioAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail{
    if ([self checkAudioAuthority]) {
        if (_success) {
            _success();
        }
    }else{
        [self showAlertController:_fail device:@"麥克風"];
    }
}

#pragma mark - 相冊權限
+(BOOL)checkAlbumAuthority{
    return [ALAssetsLibrary authorizationStatus] == ALAuthorizationStatusAuthorized;
}
+(void)albumAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail;
{
    if ([self checkAlbumAuthority]) {
        if (_success) {
            _success();
        }
    }else{
        [self showAlertController:_fail device:@"照片"];
    }
}

#pragma mark - 位置權限
+(BOOL)checkLocationAuthority {
    return [CLLocationManager locationServicesEnabled];
}
+(void)locationAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail{
    
    if ([self checkLocationAuthority]) {
        if (_success) {
            _success();
        }
    }else{
        [self showAlertController:_fail device:@"位置"];
    }
}

#pragma mark - 推送通知權限
+(BOOL)checkPushNotificationAuthority {
    return [[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone;
}
+(void)pushNotificationAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail{
    
    if ([self checkAlbumAuthority]) {
        if (_success) {
            _success();
        }
    }else{
        [self showAlertController:_fail device:@"通知"];
    }
}

#pragma mark - 通訊錄權限
+(BOOL)checkAddressBookAuthority {
    return ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized || ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined;
}
+(void)AddressBookAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail{
    
    if ([self checkAddressBookAuthority]) {
        if (_success) {
            _success();
        }
    }else{
        [self showAlertController:_fail device:@"通訊錄"];
    }
}


最後有些時會遇到不彈出權限提示,或需要在提示框增加詳細描述的時候,需要手動在info.plist加一些字段。


NSLocationWhenInUseUsageDescription    位置權限 使用期間 狀態

NSLocationAlwaysUsageDescription    位置權限 始終 狀態


下面這些我並沒有都試,所以也不知道是否正確....

NSLocationUsageDescription    用於訪問位置權限

NSCalendarsUsageDescription    用於訪問日歷權限

NSContactsUsageDescription    用於訪問聯絡人

NSPhotoLibraryUsageDescription    用於訪問相冊

NSRemindersUsageDescription    用於訪問提醒



  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved