Bug 157431

Summary: WKActionSheet tries to present on wrong view controller
Product: WebKit Reporter: Kevin Ballard <kevin>
Component: WebKit2Assignee: Nobody <webkit-unassigned>
Status: NEW ---    
Severity: Normal    
Priority: P2    
Version: Safari 9   
Hardware: iPhone / iPad   
OS: iOS 9.3   

Description Kevin Ballard 2016-05-06 14:14:29 PDT
When WKActionSheet is presented, it uses `[[_sheetDelegate hostViewForSheet].window rootViewController]` as the view controller to present from. But this doesn't work if the webview is actually inside a modally-presented view controller. This results in the sheet never showing and the following logged to the console:

Warning: Attempt to present <WKActionSheet: 0x7fbdac077a00> on <RootViewController: 0x7fbda8d7b340> whose view is not in the window hierarchy!

I imagine there's also issues with the rotation behavior, since it looks like it's supposed to dismiss itself and re-present after rotation.

The correct fix is to replace -hostViewForSheet with a method that returns the host UIViewController instead, but if that's problematic a quick fix would be to walk the presentedViewController chain to find the leaf controller and present on that (but this approach will have issues with view controllers that aren't fullscreen).
Comment 1 Kevin Ballard 2016-05-06 14:17:23 PDT
Another possible fix is instead of getting the window of the host view, it could instead walk the nextResponder chain until it finds a UIViewController and present on that. This is probably a better solution than walking the presentedViewController chain (and probably what the hypothetical `[_sheetDelegate hostViewControllerForSheet]` would have to do anyway since WKWebView doesn't know what view controller it's inside of).