相機權限和相冊權限 使用block 判斷

 

 

 

 

 .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  設置是否須要彈框,是否須要本身寫提示文字

相關文章
相關標籤/搜索