Skip to main content

The Wonderful World of Popups and Popovers in Swift: Beyond the Newly Deprecated


The Wonderful World of Popups and Popovers in Swift

To those approaching iOS alerts and popovers for the first time, or perhaps after a break, things can be a bit mind boggling. The docs tell you to use a UIPopoverController to present a UIImagePickerController, but the UIPopoverController docs tell you that it has been deprecated in iOS 9 and to use UIPopoverPresentationController instead. But a view controller doesn't recognise a UIPopoverPresentationController as a view controller, so it can't present it using its presentation method. Argh!

Finally, you get your head around the logic of UIPopoverPresentationController and then you want to figure out how to display a UIAlertView, but that was deprecated in iOS 8. So you use UIAlertViewController with a style of UIAlertControllerStyleAlert, which turns out to actually need to be written UIAlertControllerStyle.Alert in Swift.

But actually you don't want an alert in the centre of the screen, you want it to popover from a button. So you turn to UIActionSheet, but it was deprecated in iOS 8.3 in favour of UIAlertController as well. OK, it's easy enough to implement because you just use a UIAlertController with .ActionSheet style. But then your app crashes because you haven't fulfilled the UIPopoverPresentationController requirements the same as you needed to implement with your UIImagePickerController. You fix it and all works.

Now you want to open a document in another app, so you turn to UIDocumentInteractionController, which has been around for ages and amazingly isn't deprecated, but wait all the methods that it inherits from UIActionSheetDelegate are deprecated. Does this matter? I don't think so because it's pretty self-contained, but wait there's the more recent UIActivityViewController. Should we be using this or maybe both together?

Further areas to explore

So now we're fairly comfortable with the basics of popovers and alerts, and document interaction, and have our fingers-crossed that Apple don't see fit to make too many new changes. And we start looking back at image picking in greater depth and uncover other approaches
  1. The Photos Framework Reference: "To create a fully-customized image picker for browsing the photo library, use classes from the Photos framework." (iOS 8+)
  2. AV Foundation framework: "To perform fully-customized image or movie capture, ... use the AV Foundation framework as described in Media Capture and Access to Camera." (iOS 4+)
Interesting for sure but you'll want to explore AVPlayerViewController if you're going to play a movie and not MPMoviePlayerController, because that's now (you guessed it) deprecated. And so we're led on a whole new set of approaches in this area too (but I'll leave that for another post).


Endorse on Coderwall

Comments