1. 程式人生 > >iOS 第一次安裝應用,拒絕相機呼叫,頁面卡死的解決方案

iOS 第一次安裝應用,拒絕相機呼叫,頁面卡死的解決方案

void (^allowBlock)() = ^{

UIImagePickerController *imagePicker = [[UIImagePickerControlleralloc] init];

        imagePicker.delegate = self;

        imagePicker.allowsEditing = YES;

        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

        if (iOS8) {

            imagePicker.

modalPresentationStyle = UIModalPresentationCurrentContext;

        }

UIViewController *ctl = [[[UIApplicationsharedApplication] windows]lastObject].rootViewController;

        [ctl presentViewController:imagePicker animated:YEScompletion:nil];

    };

NSString *mediaType = AVMediaTypeVideo;//讀取媒體型別

AVAuthorizationStatus authStatus = [AVCaptureDeviceauthorizationStatusForMediaType:mediaType];//讀取裝置授權狀態

if (authStatus == AVAuthorizationStatusNotDetermined) {

        [AVCaptureDevicerequestAccessForMediaType:AVMediaTypeVideocompletionHandler:^(BOOL granted) {

dispatch_async(dispatch_get_main_queue(), ^{

                if (granted) {

                    //第一次使用者接受

                    allowBlock();

                }else{

                    //使用者拒絕

                    showAlert(@"已拒絕授權,開啟相機失敗");

                }

            });

        }];

        return;

    }

if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){

showAlert(@"相機許可權受限,請在設定中啟用");

        return;

    }

    allowBlock();