iOS各种权限判断(相机,相册,定位,录音)

在iOS应用中,我们经常要做各种权限的判断,以下是我整理的(相机,相册,定位,录音),希望对大家有所帮助。


1 相机权限

            NSString *mediaType = AVMediaTypeVideo;
            AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
            if(authStatus == ALAuthorizationStatusRestricted || authStatus == ALAuthorizationStatusDenied){
                //无权限
                NSString *tips = [NSString stringWithFormat:@"请在iPhone的”设置-隐私-相机“选项中,允许%@访问你的手机相机",NSLocalizedString(@"AppName",@"GMChatDemo")];
                [UIAlertView showWithTitle:@" " message:tips cancelButtonTitle:@"好" otherButtonTitles:nil tapBlock:nil];
                return;
            }



2 相册权限

            ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
            if (author == kCLAuthorizationStatusRestricted || author ==kCLAuthorizationStatusDenied){
                //无权限
                NSString *tips = [NSString stringWithFormat:@"请在iPhone的”设置-隐私-照片“选项中,允许%@访问你的照片",NSLocalizedString(@"AppName",@"GMChatDemo")];
                [UIAlertView showWithTitle:@" " message:tips cancelButtonTitle:@"好" otherButtonTitles:nil tapBlock:nil];
                return;
            }



3 麦克风权限(录音等)

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



4 定位权限

    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if (kCLAuthorizationStatusDenied == status || kCLAuthorizationStatusRestricted == status) {
        [self showHint:NSLocalizedString(@"location.open","please open location")];
        return;
    }




你可能感兴趣的:(ios,录音,相册,相机,系统权限)