美文网首页
iOS相机,相册,定位,麦克风权限判断

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

作者: 薄凉_简书 | 来源:发表于2017-05-12 14:31 被阅读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 {
            //获取权限
        }
    

    相关文章

      网友评论

          本文标题:iOS相机,相册,定位,麦克风权限判断

          本文链接:https://www.haomeiwen.com/subject/dvwztxtx.html