Lê Khánh Vinh
Lê Khánh Vinh

Reputation: 2611

"Application tried to present a nil modal view controller on target" Error on iOS7 and below

Hi I'm migrating old iOS project I got this error from my new line of code from helper class ApiManager.m (a network call)

UIAlertController *alert = [UIAlertController alertControllerWithTitle:NO_INTERNET_ERROR_TITLE message:TRACKED_ITEM_NOT_FOUND_ERROR preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:ok];
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];

The error is:

016-08-16 13:52:49.955 SingPost[967:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present a nil modal view controller on target .'

The problem only occur on iOS 7 and below. From version 8 and above nothing wrong. How to fix this? Any help is much appreciate. Thanks!

Upvotes: 0

Views: 1046

Answers (2)

iAnurag
iAnurag

Reputation: 9346

As per iOS version you should make use of UIAlertView and UIAlertController:

if(SYSTEM_VERSION_LESS_THAN(@"8.0"))
 {
   alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Invalid Coupon."  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
 }
        else
       {
        UIAlertController *alertController = [UIAlertController    alertControllerWithTitle:@"Warning" message:@"Invalid Coupon." preferredStyle:UIAlertControllerStyleAlert];


        UIAlertAction *okAction = [UIAlertAction
                                   actionWithTitle:NSLocalizedString(@"OK", @"OK action")
                                   style:UIAlertActionStyleDefault
                                   handler:^(UIAlertAction *action)
                                   {

                                   }];

        [alertController addAction:okAction];

        [self presentViewController:alertController animated:YES completion:nil];
    }

Upvotes: 1

iSashok
iSashok

Reputation: 2446

The problem occur on IOS 7 because UIAlertController as a doc says https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertController_class/

Available in iOS 8.0 and late

For iOS 7 you need use UIAlertView

Upvotes: 1

Related Questions