How can I understand if the IOS application goes background? - iphone

In my app I am increasing the brightness of the screen at some part and of course I need to return it back to its value before exiting my app. But I can't catch if the user uses the button and puts the app to the background. Is there any way to understand if the app will work on background so at that point I can change screen brightness to its old value.

You need to implement your code by using the following AppDelegate methods, and this is the way to go
- (void)applicationDidEnterBackground:(UIApplication *)application
- (void)applicationWillEnterForeground:(UIApplication *)application
- (void)applicationWillResignActive:(UIApplication *)application
- (void)applicationDidBecomeActive:(UIApplication *)application

For sake of completion I will add that you can do it in your application delegate like others have already said so.
But some time it don't make sense to do this in the application delegate, and for those case there is NSNotification for that like : UIApplicationWillEnterForegroundNotification. You will find those notification at the bottom of the UIApplication class reference.

Implement code in
- (void)applicationWillResignActive:(UIApplication *)application
and - (void)applicationDidBecomeActive:(UIApplication *)application
in your appDelegate class

Related

detect my apps been killed

I have built a simple apps that connect to a sqlite database, and the app uses storyboards for UI. On first time, it will throw up a login view. If my authenticate was successful, I change my status in the sqlite database to 1. By default, it is 0. After login, I can use the app.
For example if I kill the app, how can I know my app has been killed? I need to change the status become default again. so whenever I click on the app again, I have to sign in again.
Any idea how to do this? thanks.
Your application delegate implements several methods which handle state changes in your application. From the UIApplication.h header.
- (void)applicationDidFinishLaunching:(UIApplication *)application;
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions NS_AVAILABLE_IOS(6_0);
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions NS_AVAILABLE_IOS(3_0);
- (void)applicationDidBecomeActive:(UIApplication *)application;
- (void)applicationWillResignActive:(UIApplication *)application;
- (void)applicationWillTerminate:(UIApplication *)application;
You can implement whichever methods are relevant, in this case the applicationWillTerminate method to change state. Alternatively, always throw up a login view in applicationDidBecomeActive. (This doesn't, however solve the issue updating the database. If the database is local, there's no problem, simply change the database before resigning or terminating. The challenge here would be if your database lives on a server. Although it might bot be an issue, I could see connection timeouts affecting the integrity of the remote values.)
These methods are added to your project automatically in your application delegate, which can be found in the AppDelegate.h & AppDelegate.m files.
For a complete explanation, have a look at the UIApplication Delegate protocol reference.
You can perform close down actions in,
- (void)applicationWillTerminate:(UIApplication *)application
{
// Log out?
}
You can also do the same if your app isn't killed but just gets suspended, such as during an incoming call,
- (void)applicationWillResignActive:(UIApplication *)application
{
// Log out?
}
or,
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Log out?
}
Alternatively, on resume,
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Log in?
}
or,
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Log in?
}
You can't detect if you app was killed, since the process of you app gets killed by the system and no code is called to inform your app that it has been killed.
What you want is to set te some kins of boolean in :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Since this is called if you restart the app after it has been killed.
For the other states seen the answer by Moshe
EDIT
use the delegate methods
such as:
applicationWillResignActive

Detect if app got focus due to UILocalNotification

I'm making an app that keeps track of some reminders that repeats with an user defined interval.
I've made it so when the alert displays, the action title says "Renew". When you click this, the app opens, and here I want to create the next reminder, but the problem is that I don't know if the app opens because the notification button was tapped or if the notification fired while the app was running.
Anyone got any ideas?
The 'correct' way to do this is to examine your NSApplication's applicationState property in the application:didReceiveRemoteNotification: method of your delegate.
From the documentation for handling local notifications:
iOS Note: In iOS, you can determine whether an application is launched
as a result of the user tapping the action button or whether the
notification was delivered to the already-running application by
examining the application state. In the delegate’s implementation of
the application:didReceiveRemoteNotification: or
application:didReceiveLocalNotification: method, get the value of the
applicationState property and evaluate it. If the value is
UIApplicationStateInactive, the user tapped the action button; if the
value is UIApplicationStateActive, the application was frontmost when
it received the notification.
This is similar to your solution using flags set in applicationWillEnterForeground and applicationDidBecomeActive but with system support.
I don't know if my question was unclear but it seems that I got 4 different answers that all misinterpreted my question :P
However, I discovered that the didReceiveLocalNotivication happens between willEnterForeground and didBecomeActive, so I did this to determine if the app was already open or not:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(#"Opened from notification? %#", wasInactive ? #"yes!" : #"no!");
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
wasInactive = YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
wasInactive = NO;
}
Look up the documentation for UIApplication launch option keys. The last parameter to your application:didFinishLaunchingWithOptions: delegate method contains the information you need.
Also, look at application:didReceiveLocalNotification.
You're looking for
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
or
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
Documentation
If your application is already running you'll get this delegate message on the app delegate
application:didReceiveLocalNotification:
If it wasn't running you'll have to use
application:didFinishLaunchingWithOptions:
You need to respond appropriately in both methods to cover all cases
UPDATED
To detect if the user activated the action button requires a little more complexity. We can tell that application:didFinishLaunchingWithOptions: will have the local notification as a launch option, but it's more difficult with the application:didReceiveLocalNotification:.
Since the application becomes active after the user taps the button, we have to defer until we see that message (or not). Set an NSTimer in application:didReceiveLocalNotification and cancel it in didBecomeActive. That means the user pressed the action button. If the timer isn't cancelled the user was inside the app when it fired.
In your app delegate in this method:
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
You have to examine the launchOptions looking at this key:
UIApplicationLaunchOptionsLocalNotificationKey
When you are already active this will be called:
- (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

in iOS 4.3, how i can differentiate between the background mode, when you press the home button or when you press the on/off button?

I already use the:
(void)applicationDidEnterBackground:(UIApplication *)application {}
method, but I can't differentiate if is because press the home button or the on/off button.
Thanks in advance,
For the on/off button(or an incoming call or SMS):
- (void)applicationWillResignActive:(UIApplication *)application
For the Home button:
- (void)applicationDidEnterBackground:(UIApplication *)application
With the notification of applicationWillResignActive, applicationDidBecomeActive will still enter while you are entering in background. But there is a way to differentiate by getting the state of the app, so try this in applicationDidEnterBackground.
- (void)appHasGoneInBackground {
bool inBackground = [UIApplication sharedApplication].applicationState == UIApplicationStateBackground;
// lockScreen state
if (!inBackground) {
// do something
}
}
Apple's UIApplication-class reference
Use - (void)applicationDidEnterBackground:(UIApplication *)application {} when your app is entering the background (home button) and - (void)applicationWillTerminate:(UIApplication *)application when it's about to be closed (on/off button or iOS call to close after a random time in background).
My understanding is that when you lock or unlock your iOS device your application delegate will call - (void)applicationWillResignActive:(UIApplication *)application and - (void)applicationDidBecomeActive:(UIApplication *)application, respectively. Locking and unlocking are similar to receiving an interruption like a phone call. Sending your application to the background by hitting the home button calls different methods, namely - (void)applicationDidEnterBackground:(UIApplication *)application and - (void)applicationWillEnterForeground:(UIApplication *)application.

Save the application state when it has been deleted from the background

I am using following functions in my App delegate
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(#"applicationWillResignActive");
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(#"applicationDidEnterBackground");
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(#"applicationWillEnterForeground");
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(#"applicationDidBecomeActive");
}
- (void)applicationWillTerminate:(UIApplication *)application {
NSLog(#"applicationWillTerminate");
}
All functions are working fine. But When i Delete the app from the Background by clicking "-" red button in the background tasks, and again open the app. no function has been called. What should i use instead of all the above functions..have any ideas?
What exactly i need is ..i need to save the application state when it has been deleted from the background using "-" red button and restore it when ever it opened.
You should save the state of the application when it enters background mode (-applicationDidEnterBackground). No delegate methods are called when a background app is terminated.
You should find a lot of useful informations about this in the iOS Application Programming Guide.

- (void)applicationWillResignActive:(UIApplication *)application never gets called

I'm trying to handle phone calls and standby and things of that nature. I added the function
- (void)applicationWillResignActive:(UIApplication *)application
and
- (void)applicationDidBecomeActive:(UIApplication *)application
to my UIApplicationDelegate. When coming out of standby, "applicationDidBecomeActive" always gets called. However the issue is "applicationWillResignActive" never gets called.
I was wondering if anyone has run into this issue and whether or not anyone found a reason.
EDIT
More info, I discovered that my engine's update loop that gets run from applicationDidFinishLaunching was causing me to miss the message. I call
while(CFRunLoopRunInMode(kCFRunLoopDefaultMode, .002, FALSE) == kCFRunLoopRunHandledSource);
to catch all iphone messages but it doesn't seem to catch the resignActive message before becoming inactive.
Attempting to fork a thread for my update loop is causing weird crash bugs. Anyone have any quick fix suggestions?
Its getting called in iOS 4.0 , when the Home button is hit.
The following delegate methods are called when the Home button is hit in iOS 4.0
- (void)applicationWillResignActive:(UIApplication *)application
{
NSLog(#"Application Did Resign Active");
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(#"Application Did Enter Background");
}
And when you double tap the home button and again relaunch the App , the following delegate methods are called .
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(#"Application Will Enter Foreground");
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(#"Application Did Become Active");
}
I don't think
- (void)applicationWillResignActive:(UIApplication *)application
is called when a phone call is received. I think the OS waits for the user to either answer or declines the phone call. If it i declined, then the app says alive and
- (void)applicationDidBecomeActive:(UIApplication *)application
is called. If it is answered, then your app is told to exit and it will receive
- (void)applicationWillTerminate:(UIApplication *)application
Be sure to allow
- (void)applicationDidFinishLaunching:(UIApplication *)application
to return before running your game loop. One technique is to use the function
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay
on the application delegate and run your game loop after a delay of say ".01" After you do this, the message should be able to fire properly. I believe the reason for the message stomping was because the run loop was stuck on applicationDidFinishLaunching and wasn't able to push any other UIApplicationDelegate messages onto the queue.
- (void)applicationWillResignActive:(UIApplication *)application {
/*
Sent when the application is about to move from active state 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.
*/
}