iPhone Application Should Close, not go to Background - iphone

I have an application wherein when the user taps iPhone's central button, the application is sent to background, but I want it to be closed.
I can hand event and close it, but may be there is some configuration setting to deny running in the background?
Thank you

If you want your app to terminate when the user presses the home button, set the value of UIApplicationExitsOnSuspend to YES in your app's Info.plist file. If you do this, when the user taps the home button the applicationWillTerminate: method of your app delegate will be called and then your application will terminate.

Related

To show an alert when application just enter into background

I just want to show an alert when user just quit the application before application entering the background, how can I do that??
If I show the alert in applicationDidEnterBackground:(UIApplication *)application method then this alert will be shown when we again resume the application, but I need to show the alert before the application enters the background.
You can't do this, and rightly so. When the user presses the home button, they go to the home screen. This is a fundamental part of the iOS user experience. Nothing stops the home button working.
applicationWillResignActive and applicationWillTerminate may be of interest to this general question—but you can't present anything into the UI once the home button is pressed. These methods do let you briefly run any critical code however, while the home screen is being presented to the user.

iPhone app startup

How do I make my iPhone app start at the same place each time, i.e. my 'home' screen? I do NOT want the user to return to where they were last time they played - right in the middle of gameplay - but that's what's happening.
Thanks in advance for any tips!
You need to set the UIApplicationExitsOnSuspend key in your info.plist file to YES. Then the app will quit when the home button is pressed, and launch fresh when it is opened again.
This is presumably only happening because your app isn't really being stopped - it's simply being backgrounded. (If you double click the home button whilst viewing springboard does it show up at the bottom? If so, it's still running.)
You can disable this behaviour (so your app quits when the user hits the home button) by setting the UIApplicationExitsOnSuspend key (known as "Application does not run in background" within Xcode) in your info.plist file. See Apple's Information Property List Key Reference docs for more information.
Alternatively, you could simply capture the applicationDidEnterBackground: UIApplicationDelegate method in your app's add delegate and handle the situation from there programatically. (i.e.: Do whatever you need to do to reset your app back to the 'home' screen, etc.)
- (void)applicationWillResignActive:(UIApplication *)application {
[self.navigationController popToRootViewControllerAnimated:YES];
}
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
Set UIApplicationExitsOnSuspend to YES int the Info.plist.

How to close an application programmatically when the user taps on a button

I need to close the application whenever user taps on the button(i need to keep IBAction for closing the app).Like in games menu we have exit button when we tap on it we come out from the game.Same thing i need.How can i do this .Thanks in advance
Please see:
How do I programmatically quit my iPhone application?
WARNING: It is possible to quit the
application by calling exit.
Applications calling exit will appear
to the user to have crashed, rather
than performing a graceful termination
and animating back to the Home screen.
Such usage provides a negative
experience and is strongly
discouraged.
Instead of it, If you want your app to terminate when the user presses the home button, set the value of UIApplicationExitsOnSuspend to YES in your app's Info.plist file. If you do this, when the user taps the home button the applicationWillTerminate: method of your app delegate will be called and then your application will terminate.
exit(0); will terminate your application, but as i know we can not call exit(0); or terminate in an iPhone application. Instead we can put an alerview without button, "saying please quit the application".
This is the best way to do this
UIApplication *myapp = [UIApplication sharedApplication];
[myapp performSelector:#selector(suspend)];
Include this code where ever you need thats it. But still not recommended by apple
Developer apple link
There is no API provided for gracefully terminating an iPhone application. Under the iPhone OS, the user presses the Home button to close applications. see link text
There's one non-recommended way that crashes the app:
[[UIApplication sharedApplication] sendAction:SIGKILL to:[UIApplication sharedApplication] from:self forEvent:nil];
The app will not close the proper way or save. It's more recommended to use a UIAlertView with no buttons to force the user to close the app.
exit(0) is not apple standard way to exit the app and apple highly discourages it.Though, sometimes it approves some application.Give proper UI to show if any functioning is not working or give suggestions regarding it to user.killing app programmatically is not right way.Let user Handle the exit on their own.

Exiting or interrupted?

Is there a way to differentiate when a user is exiting an app because he/she pressed the home button or because it's receiving a phone call? in iOS 4.0 the app doesn't quit if the user answers a call but it does in 3.x.
I'd like to save my app state if the user is interrupted by a call or any other phone event but not if the user exits the app by pressing the home button.
Any advice??
The following application delegate methods get called in different situations:
applicationWillTerminate - user pressed "home" button and application is about to exit
applicationWillResignActive - user got incoming call or sms alert. if he decides to accept the call the application will quit
applicationDidBecomeActive - user ignored incoming call
applicationDidEnterBackground - user pressed "home" button and application went to background mode - applicable for platforms that support multitasking
So it seems you need to use applicationWillResignActive: method in app delegate to distinguish between your two cases

Detect Application States

I want to detect application states and send it to the server. In the new OS4, with multitasking there are some methods available to help detecting the states:
application:didFinishLaunchingWithOptions:
applicationDidBecomeActive:
applicationWillResignActive:
applicationDidEnterBackground:
applicationWillEnterForeground:
applicationWillTerminate:
I read that now, we have to use applicationDidEnterBackground instead of applicationWillTerminate. My problem is that i need them both.
When the user send the app to the background, it has the state sleep. But when the user close the app ( from the multitask bar ) the state is closed. So i need to detect both, when the user send the app to the background and when the user ( or the system ) close it.
Is there anyway or workaround to make this?
I try subscribing to UIApplicationWillTerminateNotification but it doesn´t work.
Thanks in advice.
The application will quit notification is no longer fired on iOS 4 (as I am led to believe).
When the user hits the home button, the app is sent to the background, and you will get the did enter background notification. But when a user closes the app from the multitask bar, or if the system closes it, the app is sent a SIGKIL message and quits immediately, firing no notifications or delegate methods.