how can we run GUI code when app is firing applicationWillResignActive: - iphone

i want to execute some GUI code ,let suppose I want to make the LED on when app fires
applicationWillResignActive: event ,
how can I do this ?
Can I delay the firing of this event until code executes or there is any other method available for doing this?

No you cannot delay this event being fired
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.
You can respond by putting your code in the application delegate method
- (void)applicationWillResignActive:(UIApplication *)application;
or by observing for the UIApplicationWillResignActiveNotification notification

Related

Quit code for watchOS

In Xcode there is a place to set actions when the interface controller is dismissed in:
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
However, my watchOS app needs to perform an action when the app quits completely i.e. the home screen is visible. Not when the watch is simply lowered and the screen is dimmed because the app is still running as a workout app and is performing actions.
Is there a way to do this?
In ExtensionDelegate, you'll find the method applicationWillResignActive.
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, etc.
You can use this method to do what you need.

Home button press , Which AppDelegate method should i use for scheduling a local notification

I would like to schedule a local notification as soon as the user hits the home button.
Which App delegate method should I use in this case :
applicationWillResignActive
applicationDidEnterBackground
applicationWillTerminate
Well I guess I shouldn't use the third one, but what is the difference between the first two ?
Is there any way to distinguish getting interrupted by a phone call/other notification and actually pressing the home button ?
Thanks in advance.
To schedule local notification you shold use applicationDidEnterBackground instead of using applicationWillResignActive because applicationWillResignActive call every time when app get some specific interruption line phone call, sms. You want to schedule notification when user press home button and in this case applicationDidEnterBackground is the appropriate place to do this.
One thing that should be remember before using applicationDidEnterBackground is that this delegate has approximately five seconds to perform any task, if any task in this delegate will take more time then os will terminate your app. You can also request for additional time for execution by using beginBackgroundTaskWithExpirationHandler and then use a secondary thread to perform a task. For more detail about application delegates follow the links -
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html
http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/
You should use applicationDidEnterBackground.
applicationWillResignActive gets called anytime your app is interrupted such as a phone call or SMS message. In this case if the user ignores these then your app will keep running in the foreground.
applicationDidEnterBackground only gets called when your app actually goes to the background.
You should do this in applicationDidEnterBackground:
applicationWillTerminate will not be
called when the user hits the home
button. With app switching this is
only sent when the user explicitly
quits the app or possibly in low
memory situations.
applicationWillResignActive is
additionally called when the app is
briefly interrupted, say by an SMS or
phone call alert. (Though if the user
then switches to Messages or Phone
app your app will eventually get a
applicationDidEnterBackground
message).
So it sounds like you're specifically interested in the point when the user taps the home button and the app goes to the background. applicationDidEnterBackground is the place.
You could also always schedule the local notification and only respond to it if the app isn't running when it occurs. Not better necessarily, just an option to consider.

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.

Will an iPhone app's applicationWillResignActive: method run on incoming call if the phone is already in standby mode?

I understand that if an iPhone app is interrupted (e.g., incoming call, user hits the "Sleep" button, etc.) its applicationWillResignActive: method is called. But is this method also called if the phone is already in sleep mode?
For example, if the phone goes into standby due to inactivity (or the user does it manually via the sleep button), the applicationWillResignActive: method is called once. Will it then be called a second time if an incoming call is received while in the sleep state?
No. I finally had some time to come back to this question and do some testing to answer it.
applicationWillResignActive is called when the screen locks (either manually by pushing the button on top of the device or automatically if you have the auto-lock feature enabled). If an incoming call is received while the screen is locked (i.e., blacked out), none of the applicationWillX or applicationDidX methods are called on the app delegate.