Application Termination - objective-c

I am running my app on a device. When I am in second view and terminate the app, the app quits but then when i launch it, it goes straight back to second view and not the first view (login view). I checked to see if the control goes to applicationWillTerminate method, but it does not go into that method. How can i make my program go into it?

This is probably due to iOS 4's multitasking. When you tap the Home button, your application is by default suspended on supported devices (if you double tap Home, you can see all suspended applications). You can set the plist key UIApplicationExitsOnSuspend to force UIKit to quit your application, rather than suspend, when the home button is pressed.
If you want to support the multitasking modes, look at the applicationDidEnterBackground: method of your UIApplicationDelegate implementation.

Related

Does didFinishLaunchingWithOptions happens after certain application "exits"?

Does didFinishLaunchingWithOptions happens after:
applicationWillResignActive
applicationDidEnterBackground
applicationWillEnterForeground
Or does it happen only after applicationWillTerminate?
And when applicationDidBecomeActive happens then? Thanks.
From the docs:
It is called after your application has been launched and its main nib
file has been loaded. At the time this method is called, your
application is in the inactive state. At some point after this method
returns, a subsequent delegate method is called to move your
application to the active (foreground) state or the background state.
It happens when the user opens your app. Followed by applicationDidBecomeActive when the app is ready to receive user events.
When the user presses the home button the following methods are called (by this order):
- applicationWillResignActive
- applicationDidEnterBackground
When the user opens your app again, and it is in background:
applicationWillEnterForeground
applicationDidBecomeActive
Finally, applicationWillTerminate is called instead of applicationDidEnterBackground on devices with iOS 3.x or earlier. Or with devices that do not support background apps (like the 3G).
application:didFinishLaunchingWithOptions:
only fires once: when your program starts up. You should typically create the main window/view controller here.

UIAlert during splashcreen

This is a two part question.
I have created a user agreement that the user must agree to when first launching the app (it is an alert with some information and agree/ do not agree button)
I call upon the method that creates this alert inside myAppDelegate.m and within the method
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
The problem is the alert pops up when the splash screen has finished loading and my first view comes up. I want this to happen during the splash screen. How would I do this?
The second question is When the users presses the "Do not agree button", I want them to exit the app so I have programmed it with
exit(0);
Is there a better way and will apple reject my app because of this?
Thanks in advance
1) You can't -- during the splash screen (your default.png) the app is loading into memory, and it cannot therefore execute any code, including presentation of a UIAlertView. That's why you don't see the alert until the splash disappears -- removal of the splash screen is the last thing that the app does before calling applicationDidFinishLoading:withOptions:.
What you can do is create a view controller that mimics your splash screen. This is easy -- you can even reuse default.png as a background if you want, though a better idea is just to present in this first view controller your agreement text and agree/disagree buttons.
As to your question re: use of exit(), it's best to avoid doing that. If the user refuses, you can simply do nothing. Of course, if you go the view controller route as I suggest, you can leave presented another opportunity for the user to agree.
Another thought is that Apple allows you to customize the EULA of your app when you upload a binary -- you could put it there and be covered.
Why not load our default.png as the background of you initial view and just handle the Alert in it's controller. you can always add another view or segue based on the answer.
The problem is the UIAlert blocks the Main thread, so it could stop your app from launching in time, and the process could be terminated.

iOS: Changing a view on backgroung does not refresh what is being seen before the app returns to foreground

Ok, I got this issue: I have an application with a login screen that is suposed to show everytime the app goes to background and return. The problem is, the previous screen appears for a fraction of second after the app return to foreground, because the system only refresh what is being seen after loading. What is need is a complete transition before the app returns to foreground. Yes, I am doing the transition on app delegate, at applicationDidEnterBackground. tried at every single other back/fore transition method, same results. The code works fine, but theres a flash of the screen before the login screen showing up.
The full code is as follows:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
if (!([LogicCore loadPass] == nil || [[LogicCore loadPass] isEqualToString:#""])) //a password is set,
{
[self.window.rootViewController dismissModalViewControllerAnimated:YES];//go back to the rootview, the login screen
}
}
I forgot about this, but you can have your app exit when the user backgrounds it. The only real problem here is they see your splash screen again while the app loads.
To have your app exit when backgrounded (suspended) put the key "Application does not run in background" - raw key: UIApplicationExitsOnSuspend to YES.
Not an ideal solution, but the only one I can find at the moment.
Try making the call in applicationWillEnterForeground: which "lets you know that your app is moving out of the background and back into the foreground, but that it is not yet active."
You're doing it after the transition has occurred (the change will be "queued" to happen after displaying the view)—you want to do it before, so you go with the will methods.

How to differentiate lock screen and home button (background multitasking) on applicationWillResignActive in the app delegate

I am writing an alarm clock app.
Please correct me if I am wrong:
On both events (lock & home button in iOS 4.x) the applicationWillResignActive: method is called. When locked my app can keep on running (forever if DeepSleepPreventer.h is used) to check if the alarm should go off. When home is pressed it has to stop working at some time (apart from some basic background calculations). So in this case I have to set a local UILocalNotification to trigger the alarm.
So my question: How to differentiate between those two events?
Thank you!
Have you tried implementing -applicationDidEnterBackground: in your app delegate?

Why does my iPhone App remember its UI state partially?

I have made a small test app with a button and a UISlider. Touching the button changes its label text.
I have added IBOutlet properties for both controls. I'm releasing all properties in viewDidUnload and also set them to nil in dealloc.
The interesting thing is now: I touch the button. Its tag is changed from "0" to "1" and, its text is updated: tag == 1 -> "B", tag == 0 -> "A". So the text says "B" now.
Then I close the App (iPhones home button) and restart it.
Still the button says "B"! How is that possible? Is the App not terminated?
Running on iOS4.1 here on an iPhone 4.
René
You're right. The app is not terminated. In iOS 4, apps are suspended when the home button is pressed. When you "relaunch" the app, it is simply moved to the foreground. This is why your application state isn't changing. Note: Once suspended, apps may be terminated by the OS without notification, so make sure to do all your saving before the app gets suspended, e.g., in applicationDidEnterBackground:.