标签:
.h文件中
+ (void)camaraCanuse:(QTCamaraCanuse)camaraCanUse camaraNotCanuse:(QTCamaraNotCanuse)camaraNotCanUse showAlert:(BOOL)showAlert showMsg:(NSString*)showMsg;
+(void)photoCanuse:(QTPhotoCanuse)photoCanUse photoNotCanuse:(QTPhotoNotCanuse)photoNotCanUse showAlert:(BOOL)showAlert showMsg:(NSString*)showMsg;
.m文件中
///底层判断 相机权限和照片是否打开
+ (void)camaraCanuse:(QTCamaraCanuse)camaraCanUse camaraNotCanuse:(QTCamaraNotCanuse)camaraNotCanUse showAlert:(BOOL)showAlert showMsg:(NSString*)showMsg{
BOOL canUse = [self camaraHadAuthorizationAndShowAlert:showAlert showMsg:showMsg];
if (canUse) {
camaraCanUse(canUse);
}else{
camaraNotCanUse(canUse);
}
}
+(void)photoCanuse:(QTPhotoCanuse)photoCanUse photoNotCanuse:(QTPhotoNotCanuse)photoNotCanUse showAlert:(BOOL)showAlert showMsg:(NSString*)showMsg {
BOOL canUse = [self checkPhotoAuthorizationAndShowAlert:showAlert showMsg:showMsg];
if (canUse) {
photoCanUse(canUse);
}else{
photoNotCanUse(canUse);
}
}
+ (BOOL)camaraHadAuthorizationAndShowAlert:(BOOL)show showMsg:(NSString*)showMsg{
NSString *mediaType = AVMediaTypeVideo;// Or AVMediaTypeAudio
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
NSLog(@"---授权状态:--------%ld",(long)authStatus);
// This status is normally not visible—the AVCaptureDevice class methods for discovering devices do not return devices the user is restricted from accessing.
if(authStatus ==AVAuthorizationStatusRestricted){
NSLog(@"Restricted,授权限制");
return NO;
}else if(authStatus == AVAuthorizationStatusDenied){
// The user has explicitly denied permission for media capture.
NSLog(@"Denied,授权拒绝"); //应该是这个,如果不允许的话
NSString *showStr = @"请在设备的\"设置-隐私-相机\"中允许访问相机。";
if (strNotNil(showMsg)) {
showStr = showMsg;
}
if (show) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:showStr
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
}
return NO;
}
else if(authStatus == AVAuthorizationStatusAuthorized){//允许访问
// The user has explicitly granted permission for media capture, or explicit user permission is not necessary for the media type in question.
NSLog(@"Authorized,授权啦");
return YES;
}else if(authStatus == AVAuthorizationStatusNotDetermined){
// Explicit user permission is required for media capture, but the user has not yet granted or denied such permission.
[AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
if(granted){//点击允许访问时调用
//用户明确许可与否,媒体需要捕获,但用户尚未授予或拒绝许可。
NSLog(@"Granted access to %@", mediaType);
}
else {
NSLog(@"Not granted access to %@", mediaType);
}
}];
return NO;
}else {
return YES;
// NSLog(@"未知的授权状态!");
}
}
+ (BOOL)checkPhotoAuthorizationAndShowAlert:(BOOL)show showMsg:(NSString*)showMsg{
ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
if (author == ALAuthorizationStatusRestricted || author ==ALAuthorizationStatusDenied)
{
if (show) {
NSString *showStr = @"请在设备的\"设置-隐私-照片\"中允许访问相机。";
if (strNotNil(showMsg)) {
showStr = showMsg;
}
//无权限
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:showStr
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
}
ECLog(@"相册没授权");
return NO;
}else{
return YES;
ECLog(@"相册授权了");
}
}
五个地方需要使用
发帖子 (包含两个)
添加案例详情
个人中心 添加头像
体检面诊
block 一行代码搞定 ,可用回调的block 不可用回调的block 设置是否需要弹框,是否需要自己写提示文字
标签:
原文地址:http://www.cnblogs.com/ios-wang/p/5315760.html