Search code examples
iphoneobjective-cerror-handlingnsassert

How to exit the app after an error in user friendly manner?


I need to protect my code against possible errors. If they arise then there's no point to run the app further, so I need to bring to the user some message and then exit the app. So, I'm checking the conditions and then bringing alert:

if (someError){    
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"No database file exist. App will close now." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  [alert show];
  [alert release]; 
}

And in the delegate method I'm closing the app using NSAssert:

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
  if (buttonIndex == 0) {
    NSAssert(0, @"closing");
  }
}

Also, I have included delegate protocol in header. However, the app just brings alert but after pressing OK it just freezes, and I'm getting some message "CoreAnimation: ignoring exception: closing". What am I missing or what other options exits?


Solution

  • You should not do that, it is against Apple HIG (Human Interface Guidelines):

    iPhone applications should never quit programmatically because doing so looks like a crash to the user.

    It is always better to provide some kind of feedback to the user about the error that occured and provide a way to restart the process without restarting the app. However, if you really, really want, you can use

    exit(0);