0

I want to show the UIAlertView when user Click the Iphone Home button( means Close the application)

I have done these Code

- (void)applicationWillTerminate:(UIApplication *)application
{
    NSString *errorString =@"Testing";

    UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Alert" message:errorString delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
    [errorAlert release];
}

But UIAlertView is not Showing. please help

Thanks in Advance

DreamOfMirrors
  • 2,147
  • 1
  • 21
  • 34

5 Answers5

2

The user aims to close your app when he/she presses the home button. Apple suggest to let him/her to that. What I want to say: Don't do that. I think it is even not possible.

dasdom
  • 13,975
  • 2
  • 47
  • 58
  • but i want to show the UIAlertView When User want to Close the App. What should i do? – Iphone Developer Jun 06 '11 at 07:28
  • Yes please don't do this. As a user I would hate that interruption when for any reason I want to exit. – JeroenEijkhof Jun 06 '11 at 07:29
  • 1
    apple does not allow this event you type alert code there then it will not work properly. this method is provided to developer to save data or do some important work while terminating. – Iqbal Khan Jun 06 '11 at 07:30
0

You can't show a uialertview when application receive SIGKILL(Exit) Command.You can call any file or background functions in applicationWillTerminate - To do that you need to set a key in your plist.

UIApplicationExitsOnSuspend - Boolean - YES.

Vicky
  • 1,095
  • 16
  • 15
0

Look at Problem with applicationShouldTerminate on iPhone:

  • The alert view is never shown because the 'show' method does not block, and therefore, the end of 'applicationWillTerminate' is reached immediately after you create the alert view and try to show it. I believe this is by design. You can't really begin asynchronous operations in 'applicationWillTerminate'.
Community
  • 1
  • 1
DreamOfMirrors
  • 2,147
  • 1
  • 21
  • 34
0
  1. applicationWillTerminate: may not be called in the newer version of iOS when pressing the Home button because the app could be only entering the background mode, not actually terminating.

  2. Even if it is actually called (either the app is really terminated, or you're moving the code to applicationWillEnterForeground:), showing the alert is useless because the alert is associated with the active app, and your app has gone inactive by the time the alert is shown! So what happened really is, the alert is gone when the user press the home button, and when they resume your app, they see a mysterious alert popping up.

Don't ask if the user wants to quit your app. This isn't the norm in iOS. Instead, save all states in applicationDidEnterBackground:, and restore them in applicationWillEnterForeground: and application:didFinishLaunchingWithOptions:, making the user feel as if the app has never been terminated.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
0

You might not want to use applicationWillTerminate but rather applicationWillResignActive. Check older posts like this one for more info.

Community
  • 1
  • 1
JeroenEijkhof
  • 2,232
  • 2
  • 25
  • 39