美文网首页
相机、通讯录权限问题

相机、通讯录权限问题

作者: 胖红Red | 来源:发表于2017-10-20 16:11 被阅读16次
    1. 检测通讯录是否授权
     + (void)CheckAddressBookAuthorization:(void (^)(bool isAuthorized))block
    {
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
    ABAuthorizationStatus authStatus = ABAddressBookGetAuthorizationStatus();
    
    if (authStatus != kABAuthorizationStatusAuthorized) {
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error){
            dispatch_async(dispatch_get_main_queue(), ^{
                if (error) {
                 NSLog(@"Error: %@", (__bridge NSError *)error);
                }else if (!granted) {
                 block(NO);
                }else {
                 block(YES);
                }
            });
        });
    }else {
        block(YES);
    }
    }
    
    2.是否开启相机权限
     - (BOOL)isOpenCamera {
    
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) {
        return NO;
    } else {
        return YES;
    }
    }
    
    注:

    如果授权成功,则进行下一步操作,否则添加提示让用户去设置里面进行设置

    进入用户设置的方法:
      /**
     *  跳转到系统设置页面,iOS8之后可用
     */
    - (void)gotoSettings{
    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString: UIApplicationOpenSettingsURLString]]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString: UIApplicationOpenSettingsURLString]];
    }
    }
    

    相关文章

      网友评论

          本文标题:相机、通讯录权限问题

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