.h文件中ide
+ (void)camaraCanuse:(QTCamaraCanuse)camaraCanUse camaraNotCanuse:(QTCamaraNotCanuse)camaraNotCanUse showAlert:(BOOL)showAlert showMsg:(NSString*)showMsg;ui
+(void)photoCanuse:(QTPhotoCanuse)photoCanUse photoNotCanuse:(QTPhotoNotCanuse)photoNotCanUse showAlert:(BOOL)showAlert showMsg:(NSString*)showMsg;spa
.m文件中rest
///底层判断 相机权限和照片是否打开orm
+ (void)camaraCanuse:(QTCamaraCanuse)camaraCanUse camaraNotCanuse:(QTCamaraNotCanuse)camaraNotCanUse showAlert:(BOOL)showAlert showMsg:(NSString*)showMsg{ci
BOOL canUse = [self camaraHadAuthorizationAndShowAlert:showAlert showMsg:showMsg];it
if (canUse) {io
camaraCanUse(canUse);class
}else{require
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 设置是否须要弹框,是否须要本身写提示文字