UIImagePickerController & view不在窗口层次结构中。

时间:2022-10-30 09:09:45

I have a simple Swift project in which:

我有一个简单的Swift项目:

ViewController A (class ViewController: UIViewController) presents ViewController B -- (class WebViewController: UIViewController, WKNavigationDelegate, CLLocationManagerDelegate, WKScriptMessageHandler).

ViewController A(类ViewController: UIViewController)呈现ViewController B——(类WebViewController: UIViewController, WKNavigationDelegate, CLLocationManagerDelegate, WKScriptMessageHandler)。

ViewController B is essentially just a WKWebView, here's its viewDidLoad():

ViewController B本质上就是一个WKWebView,这里是它的viewDidLoad():

let contentController = WKUserContentController();
contentController.addScriptMessageHandler(
    self,
    name: "eventHandler"
)

let config = WKWebViewConfiguration()
config.userContentController = contentController

webView = WKWebView(frame: CGRectZero, configuration: config)
webView.navigationDelegate = self
webView.allowsBackForwardNavigationGestures = false
view = webView

So, when ViewController B is presented, I basically just have a web browser on the entire screen.

当viewviewcontroller B出现时,我基本上只在整个屏幕上有一个web浏览器。

The problem I'm running into is that, if the User (while on a webpage) clicks a "File Upload / File Picker", first I see this:

我遇到的问题是,如果用户(在网页上)点击“文件上传/文件选择器”,首先我看到的是:

Passed in type public.item doesn't conform to either public.content or public.data. If you are exporting a new type, please ensure that it conforms to an appropriate parent type. the behavior of the UICollectionViewFlowLayout is not defined because the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.

通过公共类型。项目不符合任何一个公共。内容或public.data。如果您正在导出一个新类型,请确保它符合适当的父类型。UICollectionViewFlowLayout的行为没有定义,因为条目宽度必须小于UICollectionView的宽度,减去左和右的section insets值,减去左和右的content insets值。

The relevant UICollectionViewFlowLayout instance is <_UIAlertControllerCollectionViewFlowLayout: 0x12e30f270>, and it is attached to ; animations = { bounds.origin=; bounds.size=; position=; }; layer = ; contentOffset: {0, 0}; contentSize: {0, 0}> collection view layout: <_UIAlertControllerCollectionViewFlowLayout: 0x12e30f270>.

相关的UICollectionViewFlowLayout实例为<_UIAlertControllerCollectionViewFlowLayout: 0x12e30f270>,并附于;动画= { bounds.origin =;bounds.size =;位置=;};层=;内容偏移:{ 0 };收集视图布局:<_UIAlertControllerCollectionViewFlowLayout: 0x12e30f270>。

Then, if the User chooses "Take Photo or Video" or "Photo Library" from the iOS options list, I get this:

然后,如果用户从iOS选项列表中选择“Take Photo or Video”或“Photo Library”,我得到:

Warning: Attempt to present UIImagePickerController: 0x12d192200 on My.WebViewController: 0x12e2883e0 whose view is not in the window hierarchy!

警告:尝试在My上显示UIImagePickerController: 0x12d192200。WebViewController: 0x12e2883e0,其视图不在窗口层次结构中!

The result is that:

结果是:

  1. The photo library picker // camera app never appears.
  2. 照片库选择器//相机应用程序永远不会出现。
  3. ViewController B undergoes an animated dismiss, for some reason.
  4. 因为某种原因,ViewController B经历了一个动画解散。

Does anyone have any suggestions? I tried to provide the relevant code, but I can paste more if needed. Thank you.

有人有什么建议吗?我尝试提供相关的代码,但是如果需要,我可以粘贴更多。谢谢你!

-- UPDATE --------

- - - - - - - - - - - - -更新

Okay, so after more research, there are actually two separate things going on:

经过更多的研究,实际上有两件事正在发生:

  1. The first error is a function of how the "file picker" is handled by a mobile browser. See here ... grand scheme of things, not a big deal.

    第一个错误是“文件选择器”如何由移动浏览器处理的函数。在这里看到的……大计划,没什么大不了的。

  2. The second error Attempt to present UIImagePickerController... -- the more debilitating of the two -- is uglier.

    第二个错误尝试显示UIImagePickerController…——更让人感到虚弱的是uglier。

If a ViewController with a WKWebView is presented from another ViewController then, upon the User trying to "pick" an image (from library or camera), iOS will attempt to present the UIImagePicker from your WKWebView ViewController, which is what you want.

如果一个带有WKWebView的ViewController从另一个ViewController中显示出来,那么当用户试图“选择”一个图像(从库或摄像头)时,iOS将尝试从你的WKWebView ViewController中显示UIImagePicker,这就是你想要的。

Unfortunately, for a reason I don't yet know, the act of the User trying to "pick" an image also causes iOS to dismiss the presented view controller ... which, in this case, is your WKWebView controller :( As such, the WKWebView ViewController dismisses, so it then isn't in the window hierarchy when it attempts to present the UIImagePicker -- hence, the error.

不幸的是,由于我还不知道的原因,用户试图“选择”图像的行为也导致iOS取消了呈现的视图控制器……在本例中,它是你的WKWebView控制器:(因此,WKWebView ViewController解散,所以当它试图显示UIImagePicker时,它不在窗口层次结构中——因此,错误。

My solution (which is pretty hacky, but works for my particular use case), is as follows; I hope this helps someone:

我的解决方案(相当陈腐,但适用于我的特定用例)如下;我希望这能帮助一些人:

  1. Instantiate the WKWebView ViewController (ViewController B)
  2. 实例化WKWebView ViewController (ViewController B)
  3. Set VC B as the app's root view controller
  4. 将VC B设置为app的根视图控制器。
  5. Present VC B from ViewController A
  6. 从视图控制器A中显示VC B
  7. Dismiss VC A
  8. 把投一个

This essentially makes it so the WKWebView cannot be dismissed, sidestepping the problem.

这使得WKWebView不能被忽略,从而回避了问题。

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("Browser") as! WebViewController
UIApplication.sharedApplication().keyWindow?.rootViewController = vc

self.presentViewController(vc, animated: false, completion: { () -> Void in
    self.dismissViewControllerAnimated(false, completion: nil)
})

1 个解决方案

#1


0  

If you are doing int in viewDidLoad(){} function currently then try to move code into override viewDidAppear(animated: Bool) {} I had the same problem today and it fixed it.

如果您正在viewDidLoad(){}函数中执行int,那么请尝试将代码移到override viewDidAppear(动画:Bool){}中,我今天遇到了相同的问题,并且它修复了它。

#1


0  

If you are doing int in viewDidLoad(){} function currently then try to move code into override viewDidAppear(animated: Bool) {} I had the same problem today and it fixed it.

如果您正在viewDidLoad(){}函数中执行int,那么请尝试将代码移到override viewDidAppear(动画:Bool){}中,我今天遇到了相同的问题,并且它修复了它。