How to terminate an iPhone application through a TabBaritem in Interface Builder? - iphone

I have developed an iPhone app where there are four tab bar items and if i press the last TabBar the application should terminate. How to do it in Interface Builder?

Terminating the app must involve code. It can’t be done simply in IB. And there’s no documented and clean way to quit the app besides exit(0), see one of the previous questions on this topic.

In the action you can write "[[UIApplication sharedApplication] terminateWithSuccess];"
And in the head file you add
"#interface UIApplication (extended)
-(void) terminateWithSuccess;
#end"

No vagaries.
Never ever quit the app programmatically.

Related

How to set First viewController Screen as a Default opening screen when ever open a app

Hi I am new to iPhone development, I am developing a TabBar app it's contains 5 tabbar Items.
When I am close app at third TabBar viewController screen and again when I am opening app that time same third TabBar viewController screen only opening.
How to set App first screen as a default App opening Screen in iphone.
I need to set first screen as a default opening screen when ever open a app.
Could you please share your ideas here..
When you close the app, it doesn't KILL it, it just suspends it. Unless you set the option to kill it when you close it.
I suggest you read Apple Documentation For Application Flow
And possibly read the other documents Apple have provided for you there if you are new to all of this.
In your application's Info.plist, add a boolean key UIApplicationExitsOnSuspend with the value YES.
This will always exit the app (instead of running in background) when you close it. On opening app again, first view controller will appear instead of where you left it last time.
This is because you are setting your application to be run in background, so whenever you close your app the app will run in background and resume on startUp.
so just do one thing
Go to your info.plist and set value for
Application does not run in background to YES
If application is killed by default it loads all settings in Application Delegate method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Here you can modify your root view controller, which I presume is some kind of tab bar view controller.
So there is probably some code in that method which defines which tab to load.
However if you want to show specific tab after application enters in the foreground use Application delegate method:
- (void)applicationWillEnterForeground:(UIApplication *)application
Or
- (void)applicationDidBecomeActive:(UIApplication *)application
When exiting the app by for example pressing the home button on the iPhone the app is not terminated. Instead the execution is suspended but the app will remain in memory until iOS decides to remove it. This means that when you enter the app again it will be in the same state you left it. You can see if the app is still in memory by doubletapping the home button.
When your app becomes active the method applicationDidBecomeActive: in your UIApplicationDelegate will be called. You can implement that method to set the state you want your app to be in when it is restored.
I would not recommend explicitly killing the app when it is suspended. That forces the app to be reloaded each time it is started which will degrade performance.
For more information see Apple's documentation on app states.

When to show Alert for Startup and coming out of background?

In my project I show an Alert to the user to indicate an 'empty list'.
Right now, I show it in AppDelegate>applicationDidBecomeActive.
I'm doing this because I want the alert to show if the list is empty
at app startup and when coming out of the background (iOS 4.2 through 5.x).
EDIT:
I use a method in the AppDelegate, and call it with a slight delay, and I still get this notice.
[self performSelector:#selector(checkForNoMessages) withObject:nil afterDelay:1.0];
However, this causes a "wait_fences" notice in the debugger and I'd prefer not to submit to Apple with this notice.
Where is the proper place to put a popup Alert so that it appears:
1) At App startup
AND
2) When the App is coming out of the background?
Do I need the Alert in more than one place?
I recommend writing a method in your AppDelegate or better in your root view controller which shows the message. Maybe with some arguments, so you can reuse it but that's up to you.
If you are following the MVC architecture ask your model about existing entries and trigger the Alert message if necessary. But encapsulate this behavior in a controller as well.
application:didFinishLaunchingWithOptions: and applicationDidBecomeActive: are the places where you want to delegate this task to your controller.
More about iOS Multitasking: https://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
Edit:
Don't forget that you have to call the methods from the main thread.
And do all startup stuff first.
OK - the problem wasn't where I called the alert, it was because it was in a method. Once I moved the code from a method into applicationDidBecomeActive, all is well.

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.

quit app in iOS4

I have an old app which did many UI initialization work in viewDidLoad of various views. In iOS4 home button just put the app in background so viewDidLoad won't get called when re-launching the app.
I don't want to put those initialization procedures in viewWillAppear as it is unnecessary to re-initialize the data every time the view appear.
How can I completely quit my app when user press the home button? or any simple way to reload the view controllers that sit inside a tabBarController?
thanks.
There is a key in your app's Info.plist file called UIApplicationExitsOnSuspend, set it to the boolean YES, which will essentially revert the home button's functionality to pre-iOS4 and completely exit your app when the home button is tapped.
Try disabling backgrounding. Apple has this in their documentation
Try to check.
UIApplicationExitsOnSuspend
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/Reference/Reference.html
you have to look up to the UIApplicationDidBecomeActiveNotification i think, but in the uiapplicationdelegate-protocol, there are even some other interesting things for you
or perfectly for you:
UIApplicationWillEnterForegroundNotification as a new notification in ios4 for this problem

iPhone XCode programming: execute code when change tab in a tab bar application

I'm developing a simple tab bar application for school.
It has 3 tabs.
One of them plays music in streaming and I would like to stop the music playing when the user changes the tab. Is this possible? and How? Thank you in advance!
Implement the UITabViewController delegate method:
– tabBarController:didSelectViewController:
And you should be able to do what you want. There's also
– tabBarController:shouldSelectViewController:
if you want to know before the tab changes.
As Carl says, this is possible. However, if I were to develop such application (and actually, I am), I would use the viewWillDisappear method of my UIViewController (and I am). That way you keep the logic separated between your implementations.