Action sheets presented for links/numbers don’t work in presented view controllers

Originator:steipete
Number:rdar://26295020 Date Originated:16-May-2016 11:00 AM
Status:Closed Resolved:
Product:iOS SDK Product Version:9.3.1
Classification:Serious Bug Reproducible:Always
 
Summary:
Action sheets presented by links in UITextView, UIWebView and WKWebView don’t work if there is a presented view controller. UIKit and WebKit both attempt to present these alert controllers on the root view controller, which fails and logs a message to the console if the root view controller already has a presented view controller.

Steps to Reproduce:
0. Comment out the call to PSPDFInstallWorkaroundForSheetPresentation();
1. Present a view controller whose view hierarchy contains a UITextView, UIWebView or WKWebView containing links.
2. Try to trigger an action sheet from a link, for example by long pressing on a web link or tapping on a detected calendar event link.
3. Uncomment PSPDFInstallWorkaroundForSheetPresentation(); and verify the fix

You can observe this problem running the attached sample project on any iOS device or simulator with iOS 8 or later. The project demonstrates the problem in each of UITextView, UIWebView and WKWebView in three tabs. In the project try interacting with the links on the first screens (in the tabbed bar) and on the second screens (presented view controllers shown by tapping the button in the top right).

Expected Results:
An action sheet should be presented.

UIKit and WebView should try to present the alert controller on the correct view controller.

Actual Results:
When there is a presented view controller on the window’s root view controller (on the second screens in the sample project) action sheets are not shown. Instead, a message is logged about presenting on a view controller’s whose view is not in the window hierarchy or about already presenting if the presentation is not full screen (depending on modal presentation style).

Version:
iOS 9.3.1 (13E238) Xcode 7.3.1

Notes:
The UIKit bug appears to be in -[_UIRotatingAlertController presentSheetFromRect:]. It starts from the view that triggered the action. It takes that view’s window, then the window’s root view controller and presents on that.

WebKit does the same thing: https://github.com/WebKit/webkit/blob/master/Source/WebKit2/UIProcess/ios/WKActionSheet.mm#L102

It seems likely this bug was copy/pasted from one codebase to the other.

This radar has also been reported as rdar://26294823 (without workaround) and the oldest reference I could find without access to Apple Radar was rdar://18110831 reported for iOS 8b5.

We plan on using following workaround until this has been resolved. Comments or tips welcome to make this safer:
https://gist.github.com/steipete/b00fc02aa9f1c66c11d0f996b1ba1265

(@steipete on Twitter or steipete@gmail.com)

Comments

Fixed in iOS 10b1

Find the sample under https://github.com/PSPDFKit-labs/radar.apple.com


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!