在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;
}
+ (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;
}