尝试在网页中加载摄像头时 Webview 崩溃

Webview crashes when attempting to load camera in web page

我有一个 webview 加载允许用户上传图片的页面。当用户选择 "take photo" 选项时,应用程序崩溃并显示以下警告。

警告: 试图在视图不在 window 层次结构中的 <UINavigationController: 0x156b4c400> 上呈现 <UIImagePickerController: 0x1563d8000>

这是用于加载 webview 的代码

WebViewController *webViewController = [[WebViewController alloc] initWithUrlString:urlString];
[viewController showViewControllerFullScreen:webViewController];

- (void)showViewControllerFullScreen:(UIViewController *)viewController {
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    [self.view.window.rootViewController presentViewController:navigationController animated:YES completion:nil];
}

为什么 UINavigation 控制器在用于显示 Web 视图时不是 window 层次结构的一部分?

请设置以下行

  self.window.rootViewController = navigationController;

参考

UIImagePickerController on UINavigationController whose view is not in the window hierarchy