Fixing The Dreaded ‘Invoked Recursively’ UIAlertController Issue

While working on a recent project I came across an issue that seems to have no documented fix for in any forums, apple publications, stack overflow articles or any other go to spots on the web for these issues. After searching the web for close to a week to find a fix for this (yes, it’s seriously that hard to find a fix for) I came across the suggestion to override the class of the UIAlertController itself.

Understanding the issue
It seems that apple have not really put alot of thought into the iOS 9 SDK. I say this because the issue only occurs in SDK’s greater than iOS 9 and is actually quite a simple fix that would’ve taken dev’s around 2 minutes to patch in. What’s happening is that the UIAlertController is relying on a deprecated method to get the supportedInterfaceOrientations of your app but can’t fetch them because the method no longer exists.

Fixing the issue
As mentioned above, the fix is quite simple. You will need to create the following two files in your project:

UIAlertController+supportedInterfaceOrientations.h

//
//  UIAlertController+supportedInterfaceOrientations.h

#import <UIKit/UIKit.h>

@interface UIAlertController (supportedInterfaceOrientations)

@end

UIAlertController+supportedInterfaceOrientations.m

//
//  UIAlertController+supportedInterfaceOrientations.m


#import "UIAlertController+supportedInterfaceOrientations.h"


@implementation UIAlertController (supportedInterfaceOrientations)

#if __IPHONE_OS_VERSION_MAX_ALLOWED < 90000
- (NSUInteger)supportedInterfaceOrientations; {
    return UIInterfaceOrientationMaskPortrait;
}
#else
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
#endif

@end

Now just import this file into any class that uses a UIAlertController by placing #import “UIAlertController+supportedInterfaceOrientations.h” into your imports declarations. You’ll now have a working UIAlertController and hopefully can move on with your project.

Leave a Reply