Dev:UIAlertController

UIAlertController is a new UIKit.framework class introduced in iOS 8. Even though UIAlertView and UIActionSheet have not been marked as deprecated, you should use UIAlertController to display Alerts and Action Sheets and if you are working in SpringBoard you should use SBAlertItem from the SpringBoardUI.framework framework.

What you can do using Public API
Since UIAlertView and UIActionSheet are UI Elements and UIElements should only be used within UIViewControllers and subclasses, Apple has made UIAlertController a UIViewController subclass. Both UIAlertView and UIActionSheet are UIView subclasses which allowed you to easily subclass it and modify its content. Subclassing UIAlertController is also possible, but you have less freedom to design what you need. You'd need to make your own custom Alert then. However if you are happy with the default look of an Alert, you can create one using the public API as follows:

That is basically all what you can do with public API. As you can see there is no delegate method anymore on which button was clicked but there is a convenient handler block for each action.

What you can do using Private API
Now the fun part begins, the part that actually made me write this article because the public API is accessible for everyone. Private API can only be used on jailbroken devices because Apple does not allow developers to use them hence they are private.

There is one property of UIAlertController which makes the difference and which I will pay most attention to in this article:

Yes, each UIAlertController has a contentViewController property which again has an UIView that we can customise. This allows us to create 90% custom UIAlertControllers and 10% go to the default look of it. We can add any UIKit Element to our Alert View and we have full control over it. To achieve this we will do three steps:


 * Subclass UIViewController
 * Create a UIView for our ViewController
 * Assign our ViewController as the contentViewController of the UIAlertController

Subclass UIViewController
Our subclass will not contain much code, it will only load our UIView and set a preferred content size:

Now we need to create our custom UIView which is initialised by our MyContentViewController class. In this example I will create a title label and a message label to re-create the default look of an alert. You are free to add anything you want but a title and a message are important.

Create a UIView for our ViewController
Using NSLayoutConstraints is a great way to make a clean looking interface without needing to take care of orientation changes and different device types. This seems a lot of code compared to simply showing an alert view, but while iOS manages the Alert you are free to design its content which is a great opportunity. And this shows that with one private property you are able to do so much more.

Assign our ViewController as the contentViewController of the UIAlertController
Last but not least we assign our view controller to be the content view controller of the AlertController

What about Message and Title?
There is an easy trick to keep the message and title string and use them in your custom content view. All you need to do is adding a weak property to MyContentViewController of type UIAlertController and assign the alertController to it.

And in the loadView method where you create the custom view you pass the message and title property of UIAlertController to your UIView subclass which then can use it when creating the labels. This way you can pass the title and message all the way to your content view.