iOS相机,相册,定位,麦克风权限判断

2017-05-12  本文已影响302人  薄凉_简书

1 相机权限

#import<AVFoundation/AVCaptureDevice.h>
#import<AVFoundation/AVMediaFormat.h>

AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    
    if (authStatus == AVAuthorizationStatusDenied || authStatus == AVAuthorizationStatusRestricted) {
        // 没有权限,弹出alertView
        [self showAlert];
    }else{
        //获取了权限
    }

2 相册权限

#import<AssetsLibrary/AssetsLibrary.h>

ALAuthorizationStatus authStatus = [ALAssetsLibrary authorizationStatus];
    
    if (authStatus == ALAuthorizationStatusDenied || authStatus == ALAuthorizationStatusRestricted) {
        // 没有权限
        [self showAlert];
    }else{
        // 获取权限
    }

3 麦克风权限(录音等)

- (BOOL)canRecord {
    
    __block BOOL bCanRecord = YES;
    
    if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending) {
        
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) {
            [audioSession performSelector:@selector(requestRecordPermission:)     withObject:^(BOOL granted) {
                
                bCanRecord = granted;
            }];
        }
    }
    return bCanRecord;
}

4 定位权限

#import <CoreLocation/CoreLocation.h>

CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    
    if (kCLAuthorizationStatusDenied == status || kCLAuthorizationStatusRestricted == status) {
        //没有权限
        [self showAlert];
    } else {
        //获取权限
    }
上一篇下一篇

猜你喜欢

热点阅读