How to handle home button - iPhone/iPod - iphone

I am developing a game for the iPhone. I do not actually have an iPhone, so I am testing my game on an iPod device (version 4.2.1). When I press the home button the game starts from level one. I find this odd, since in the simulator, after pressing the home button, the game starts from the same state where I stopped. I am unsure as to why the behavior is different on the iPod, maybe I need to handle the AppDelegate method differently (is it not handled automatically depending upon the device?)
How can I handle this issue?

It appears that the device you are testing your app on does not support multi tasking.
The older iPod touches and iPhones do not support multi tasking an close the app rather than suspending them.
All device that can run iOS 4.3 or higher will support backgrounding. Devices that can't update above 4.2.1 will not support backgrounding and app will be closed if you press the home button.
You will need to save the game state in the apps delegate applicationWillTerminate:
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
Save the current game state here and read them in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Related

iPhone - Need to replay background sound when it comes to foreground from background

My application plays a background music when it launches. It stops when I quit the application. But not playing the music again when I open my application. The application has 2 views and I want to play background sound on only main view. So when the app launches it should play the sound until user quit the app or when the user goes to the second view in the app. The sound should be played again only when the user returns to the main view. Currently, I am calling the sub function which plays the sound in 'viewDidLoad' function. please help.
Thanks
Try using this method in your AppDelegate:
- (void)applicationDidBecomeActive:(UIApplication *)application
DidBecomeActive Apple Documentation

How to detect when a phone call is triggered when the application is up?

My application is a coupon shopping,users will download the coupons in phone and when completed they will be directed for the automatic cash payment.
If a user downloads 5 coupons and inbetween gets a call as it is iOS4 it goes to background.
So when we press the home button also the application goes to background by this behaviour.I have save few data and restore the coupons when the user quits the application by homebutton.
But in iOS 4 behaviour homebutton press and phone call interruptions shows the same behaviour and calls the same functions,how i could differentiate between the 2.
Please this is a tedious function,please help me.......
Without multi-tasking: For applications that do not support background execution or are linked against iOS 3.x or earlier, applicationWillTerminate: method is always called when the user quits the application. 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.
With multi-tasking: You can implement applicationWillResignActive:delegate method in your app delegate, which gets called during 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.
- (void)applicationWillResignActive:(UIApplication *)application {
flag = YES;
[self doCleanUp];
}
- (void)applicationWillTerminate:(UIApplication *)application {
if (!flag) [self doCleanUp];
}
iOS will not tell an app what caused an interruption such as a phone call, an SMS message, or a press of the home button. This is a deliberate design decision by Apple. Apple expects apps to be designed to exhibit the same behavior no matter what caused the interruption.

iPhone : killing the process from multitasking

Why debugger didn't launch to - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions when I launch the App after killing my app process from multitasking ? "killing app process from multitasking" mean to make double click on "Home" button, after that, at the bottom of iPhone menu with active apps will be shown, and than I delete my app there. thanks...
If you launch your app from the iPhone screen (pressing its icon) after you have killed it, the debugger is not attached to it any more, hence it will not launch. You need to trigger the application from XCode again.
If the app's getting killed, regardless of from where, - (void)applicationWillTerminate:(UIApplication *)application will get called. application:didFinishLaunchingWithOptions: only gets called when the app launches, not when it quits…hence the names.

Is there a way in iOS to observe the home button being pushed?

I am basically trying to make the device play a sound whenever the app exits (even if the phone is being turned off, the sound would play while the button is being held down). Is this possible?
You could try the - (void)applicationWillResignActive:(UIApplication *)application UIApplicationDelegate method.
Alternately, you could listen for the UIApplicationWillResignActiveNotification notification.

Is it possible to stop the sound of a UILocalNotification from playing when the notification is delivered?

If a UILocalNotification fires with a sound set, and the user taps "Cancel" on the notification alert, the sound is stopped. But if the user taps "View", iOS then delivers the notification to the app and the sound keeps on playing. Is there any way to cancel this sound from the app? Canceling the notification in the app once the notification is delivered doesn't work (I didn't expect it to, the notification has already been delivered after all), and since I don't have the sound's system sound ID (and I didn't allocate it in the first place), I can't call AudioServicesDisposeSystemSoundID (can I?).
Is it possible to stop a UILocalNotification sound from playing if the user taps the Action button of the notification's alert?
It does not stop on the device too (5.1)
I have been trying to fix it but I can't figure it out.
I actually got it to stop on the simulator using this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
[[UIApplication sharedApplication] cancelLocalNotification:localNotif];
}
return YES;
}
but It still doesn't stop on the device
Apparently the problem only exists on the simulator (iOS 4.2 sdk), not on the actual device.
It can be handled in application delegate method as follows, if the user taps the Action button of the notification's alert, then the following method will be called, there we can cancel the notification
- (void)application:(UIApplication *)app
didReceiveLocalNotification:(UILocalNotification *)notif {
[[UIApplication sharedApplication]cancelLocalNotification:notif];
}
I had the same problem when initially testing with an iPhone 4.
The problem appears to have gone away as of iOS 8 and now Local Notification Sounds stop when canceled after transitioning to the App.
iPhone4 [ iOS - 7.1.2 ] - Local Notification sound keeps playing in App no matter what
iPhone6 [ iOS - 8.1.1 ] - Local Notification sound stop when canceled programmatically
From what I can infer, it appears a fix exists somewhere in iOS 8.x
(Alas I didn't manage to find any documentation or release notes on the matter.)
The Problem becomes a non-issue for apps focusing on iOS 8
(provided you cancel the Local Notification coming in)