How to handle phone call interrupts in iPhones below iOS 4 - iphone

In one of my application I need to handle call interrupt. I know application will directly enter background in iOS 4 onwards. How can I handle this situation devices with iOS's less than 4. Do I need to implement any delegate method to be get notified that a call interrupt has come. Please help.
Thanks in advance.. :)

Use the below Appdelegate method:
(void)applicationWillResignActive:(UIApplication *)application
This delegate will call 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 when the user quits the application and it begins the transition to the background state.
To Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. For this use the below appDelegate
(void)applicationDidBecomeActive:(UIApplication *)application

You can have a look at The Application Life Cycle. You will find there the difference between iOS 4.0 and earlier versions.

Related

Proper appDelegate method for Flurry startsession?

Flurry docs recommend placing the startSession call in applicationDidFinishLaunching:.
Two problems with this...
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
[FlurryAnalytics startSession:#"AWESOMEAPIKEY"];
// ...
}
1) Isn't application:didFinishLaunchingWithOptions: the new approved launch point?
2) This is only called once on launch, but don't we want session info every time a user opens or switches back to the app? Or does Flurry handle all that on their own by listening to some event or NSNotification?
Wouldn't a better place to put the startSession call be in applicationDidBecomeActive: or applicationWillEnterForeground:, like so?
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// ... Flurry AppCircle setup
[FlurryAnalytics startSession:#"AWESOMEAPIKEY"];
// ... your setup
}
for your case 1)
correct place to put [FlurryAnalytics startSession:#"SOMESESSIONKEY"]; is
application:didFinishLaunchingWithOptions:
you can place it there without worries. I have done this by myself and the app is working awesome at appstore and providing the stats perfectly.
for case 2), your secession will be automatically resumed when app returns to foreground so you dont have to do any special handling here.
I was real curious about this too. I looked at my inherited code for my app and didn't see any flurry activity in didbecomeactive, foreground, etc. I only saw the startsession in didfinishlaunchingwithoptions. I saw the below on the flurry site re: startsession, but i still don't get how it works, just behind the scenes stuff the flurry library does? #samfisher, can you elaborate?
"This method serves as the entry point to Flurry Analytics collection. It must be called in the scope of applicationDidFinishLaunching. The session will continue for the period the app is in the foreground until your app is backgrounded for the time specified in setSessionContinueSeconds:. If the app is resumed in that period the session will continue, otherwise a new session will begin."
FlurryApi.h shows the default as 10 for setSessionContinueSeconds so I guess Flurry handles it, I'm just looking for more confirmation.
http://support.flurry.com/sdkdocs/iOS/interface_flurry_analytics.html#a78b0b92085b38875d51f1ca0d699849a

iOS4: application termination event?

I would like to know how i can detect when an application is about to be terminated. I mean really terminated, not just going into background mode. I have used this event, and it doesn't fire :
applicationWillTerminate
What i would really like to achieve is get some kind of event or notification when the user taps Home twice and presses the red baloon on the app. I don't care about the application going into background mode, there are a couple of events that handle this properly and they all work fine.
I need this so that i can "inform" my server to stop sending push notifications to APNS for apps that are terminated and aren't running in the background.
If you know of an easier way to achieve this, i'd be glad to hear :)
Thank you
Register your object (view controller, etc.) to listen for the UIApplicationWillTerminateNotification notification, and/or override the application delegate's -applicationWillTerminate: method and put your code there.
Angel, what you're asking for cannot be done. The app will be terminated with SIGKILL. Unstoppable, not catchable, no notifications. There is no difference between a system-initiated termination or one requested by the user.
You'll get applicationWillTerminate only if your app doesn't support background processing.
From UIApplicationDelegate docs on the matter:
For applications that support
background execution, this method is
generally not called when the user
quits the application because the
application simply moves to the
background in that case. However, this
method may be called in situations
where the application is running in
the background (not suspended) and the
system needs to terminate it for some
reason.
Seems to me that unless your background process is actively doing something in the background (not being suspended) it the applicationWillTerminate method will never get called.
I guess it depends what you definition of "being in the background" is.
IIRC it goes like this:
On start:
didFinishLaunchingWithOptions
applicationDidBecomeActive
Pressing home:
applicationWillResignActive
applicationDidEnterBackground
Coming back from home screen:
applicationWillEnterForeground
applicationDidBecomeActive
applicationWillTerminate is called when your application exits due to a call or the OS kills it for some reason.

How to reload my view when my app becomes active again?

Let say someone is using my app and the change the settings in the settings bundle, when they come back to my app I would like my view to update (via my updating method) according to those settings. I've tried many different things but I just can't get it to work.
What is the best way to implement this kind of behavior for my iPhone app?
in your AppDelegate put these methods:
- (void)applicationWillEnterForeground:(UIApplication *)application {
/*
Called as part of transition from the background to the active state: here you can undo many of the changes made on entering the background.
*/
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
}
Then you have hooks to do whatever you want.
It's worth noting that you're not limited to the AppDelegate; you can listen for these events from anywhere in your code with an NSNotification. See this answer for more details on how to listen for UIApplicationDidBecomeActiveNotification.

Using the applicationWillTerminate method doesn't seem to be working

I am wanting to save some app state data for when the user is quitting the app. I am thinking that I should be using the applicationWillTerminate method inside of my appDelegate, but when I try and do anything there, it's not doing anything:
- (void) applicationWillTerminate:(UIApplication *)application {
NSLog(#"test");
}
When I run my app, and do some stuff, and hit the home button to quit the app, nothing comes over the console...
Do I have to implement the applicationWillTerminate method in the appDelegate? The user of my app will most likely be in a Reader view when they leave, is there anyway to implement the app will close method there?
see this link iPhone simulator and applicationWillTerminate() for older iOS versions;
but remember the Home button does not necessarily terminate the application is ios4... you should use applicationDidEnterBackground but would suggest both places applicationWillTerminate and applicationDidEnterBackground
Adding the UIApplicationExitsOnSuspend key to your application’s Info.plist file and setting its value to YES will make you application quit when you hit the home button, even on iOS4
In iOS 4, the applicationWillTerminate: method is only called if you opt out of the background execution model. In the standard iOS 4 application lifecycle, applications are suspended (not terminated) when the home button is pressed. Your application delegate will receive an applicationWillResignActive: message followed by an applicationDidEnterBackground: message. The applicationDidEnterBackground: method is a good place to save user data.

Retain the State of iPhone Application after exit

How can I retain the state of an iPhone after it has exited. What is the easy way to do it?
The first question is when do you save? The answer is in two places (assuming you want to support 3.x and 4.x devices).
First, for OS 3.x devices (and OS 4 devices that don't multi-task):
- (void)applicationWillTerminate:(UIApplication *)application
And second, for OS 4.x devices:
- (void)applicationDidEnterBackground:(UIApplication *)application
You need to do this on iOS4 devices because if the app is shutdown while it's in the background it is just killed; you never see the applicationWillTerminate message.
As for the how, well it depends on how complex your app is. I created a simple protocol that I implement for each view controller that might want to save its state:
#protocol SaveState
- (NSData*) saveState;
- (id) initWithSaveState:(NSData*)data;
#end
It saves the state by looping through view controllers in the main navigation controller and calling the save state method. It then does the reverse in the applicationDidFinishLaunching: method. More information on my blog.
In your application delegate, you can define the -applicationWillTerminate: method to include code to save application state data.
- (void) applicationWillTerminate:(UIApplication *)application {
// save state to data model here...
}
Your data model is up to you. For example, this could be a set of user defaults or a Core Data store.
The next time the app is started, you could check for saved state data in -applicationDidFinishLaunching: and initialize the app appropriately.
If you are using iOS 4 and your application supports multitasking features, you will get some of the state saving functionality "for free" because the app resigns focus, instead of terminating.