How to detect whether user didEnterBackground because of phone call or click on home button? - iphone

We have a multiplayer game and sometimes user quit the game so we would like to know how did user quit the game. Can we use didEnterBackground for that?
We would like to know whether user quit the game by clicking exit button, or by incoming call, or by home button to exit.

When you press home button your application is sent to background and applicationDidEnterBackground will get call first and if it is by call, sms then applicationWillResignActive will get call first.
This is not perfect answer of your question but this might help you in some.

in the appDelegate.m File u have this function:
- (void)applicationDidEnterBackground:(UIApplication *)application
write the code you want in it. (saving data or anything)

First of all, the user interface guidelines forbid quitting an app programmatically and based on what I have read, apps the provide an exit button will be rejected.
If by 'quit the game' you mean something along the lines of 'terminated gameplay', then that should not be a problem.
Also note that pressing the home button does not exit the app. It simply sends it to the background. There does not appear to be any facility to detect why the app left the foreground, which probably means that Apple wants your app to handle all such cases in the same way: by saving state and being prepared to restart where execution left off. Note that an application in the background can be terminated at any time, hence the need to save state.
The answer to your question therefore would seem to be: don't do that.

Related

Is there a method if application was closed in multitasking?

Is there a method like: wasTerminated or something like that? I want to display the user an alertsheet, if he has completely closed the app from multitasking. At the moment I have implemented my code in the DidEnterBackround, but is there another way to fix this? That would be very useful for me.
Sorry I'm a newbie and I didn't find a solution on the net.
Thanks.
Background applications can be terminated at any time, and there's no way for an app to tell whether it was terminated by the user (through the multitasking switcher) or by the system.
Your only notifications are –applicationDidEnterBackground:, –applicationWillEnterForeground:, and –application:didFinishLaunchingWithOptions:. With these notifications you can tell if some background task completed or not, but you can't tell how your app was terminated.
Do you want to show the message to the user, next time the user starts your app, or when the app is killed. If the app is killed by double clicking on the home button and killing it, and if you want to find out then, it is not possible.
But if the app is killed and the user starts the app again, then you will know whether the app is started fresh or did it become active.
The method
-(void)applicationWillEnterForeground:(UIApplication *)application{
NSLog(#"Entering foreground");
}
will get called if it becomes active from background.
Other wise,
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
will get called where you can put your alert view.
You can easily experiment with these situations and figure out a way to do what you want.

Programming background behavior when iPhone is locked and user presses power button

Sorry, I am new to iPhone development and my google searches have failed me, so I have had to resort to posting a potentially idiotic question on SO.
I would like to write an app that, when suspended, performs an action when the user presses the home or power button (presumably to unlock the phone). I read the section in the iOS App Programming Guide's App States and Multitasking and the "Processing Queued Notifications at Wakeup Time" discusses handling queued events upon waking up. However, this isn't what I'm after.
I'd like to know if it is possible to:
From the phone sleeping state (I couldn't find a document for phone states, so I am talking about the case where the user presses the power button to turn off the screen), I would like my app to be ready to respond to the event where the user presses the power or home button (to unlock the phone)
I would like my app to respond to the event that occurs when the user unlocks the phone. I found an answer here that's close.
I don't want any funkiness when calls come in, get ignored, etc. :)
From what I can tell, it's a very gray area in the API around locking and unlocking, and I'd like to verify whether or not I'm wasting my time trying to do this.
It looks like I can use the accelerometer to detect when the phone is locked, but I also assume that I won't be able to count on this behavior in all future versions of iOS.
EDIT - I think I can handle the locking and unlocking requirements by assuming that the application has to be running at the time the phone is locked and unlocked, but I still cannot figure out if it is possible to determine #1 above, which is that the power button has been pressed and the unlock screen is displayed. Likewise, I'd like to know if the power button has been pressed again and is no longer displayed (i.e. screen is off).
All of the behaviors for your app being suspended/backgrounded are in the document you referenced. The AppDelegate will receive these messages and pass them on to whatever view you want to listen for them.
-applicationWillTerminate
-applicationWillEnterBackground
-applicationDidBecomeActive
-applicationWillResignActive
You can set up a notification observer in your view if you would like the view to be notified of any of these events. Then just override them or set up a custom method to do any work that needs to be done.
While the Apple docs might be a little foggy at first, all the information is in there to let you know which state your app will go to and how it will be handled. It is up to you to figure out what your app needs to do for each of these events. Hope this helps.

How to handle the phone call while running the application in iphone

I am doing with a paint application. while drawing any picture if I got any phone call, the application is getting quit and reopens immediately after end of the call. But my requirement is I should able to draw the picture while I am in incoming call.
Any one can Please help me out
When a phone call comes in, and the user picks it up, they will always leave the current app and the phone app will start. I don't think you can change this behaviour.
While the user is in the phone call, though, they can change to another app simply by pressing the home button and starting another app from the home screen, or by double-pressing the home button and switching to another app, including yours.
In that case there will be a thin view below the status bar indicating that they're in a call, so you have to make sure your app resizes properly to make space for that.
This is default from apple that any application go inactive if incoming call is in active.We cannot change this.
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
)
To Restart any tasks that were paused (or not yet started) while the application was inactive. For this use the below appDelegate
(void)applicationDidBecomeActive:(UIApplication *)application
For more visit app multitaking
Hope, this will help you..

iOS home button warning, is it possible?

I really don't think this can be done, but still my boss wants me to provide a link where it says so. What he wants is to add an 'are you sure you want to exit?' warning when the user presses the home button, and if the user says 'no' the app won't go inactive.
It can't be done, can it?
No, you cannot do this - the application has no say in this. Ask your boss whether he has ever seen a single example of an iOS application that would do this. There isn't ... not one I would bet.
The application can continue to execute some functionality in the background - streaming music, getting location information for example, but no application can block the home button. If you could do this, you could block an application from ever closing.
A) You couldn't technically do this and
B) Apple wouldn't allow it to be released on the App Store if that was the distribution route you were taking
If you look at the methods stubs created by XCode when you create an application delegate
-(void)applicationWillResignActive:(UIApplication *)application
-(void)applicationWillTerminate:(UIApplication *)application
That are filled will comments about how you can use this method to pause tasks, disable timers, throttle down frame rates, save data - there is nothing about being able to delay, query the user with an "Are you sure" message.
This whole idea is rather counter to the user-experience of the iPhone/Pad/Pod-Touch.
From the App Store guidelines (slightly abbreviated):
Apps that alter the behavior of switches on the device will be
rejected
This is a proposed change the behavior of the home button - so would be rejected.
This is possible on a jail broken device, using un-aproved API's. The concept is in multiple violations of apple's usage policy however so you would never, ever, ever get an app attempting to implement this in any way on the official app store. Here's just a few reasons:
You can't alter the functionality of any buttons (including the volume buttons, some camera apps used to use them to take pictures, but they got booted from the store as a result).
You can't interfere with standard user interactions with the device. The home button takes people home, you can't prevent that, or ask for confirmation as that would be interfering with the interaction.
There is no public API to detect actual usage of the home button. As such you would need a private API, and you can not use private API's without explicit permission from Apple, which they would never give due to #1 and #2 above.
I'm sure there's plenty of more reasons, but regardless it would be in direct violation of app store policies as well as iOS human interface guidelines.
You can detect when the app is about to lose focus, has lost focus, or could loose focus (such as a phone call is coming in) but you can not alter the flow (i.e. not allow the app to lose focus).
You can continue to execute code in the background within the backgrounding guidelines and limitations. The backgrounded code could submit a notification to the user that would allow them to switch back into the app... that's about as close as you could get, and expect apple to reject you if it happens every time the app closes...
Already answered by numerous others, but no, you can't do this. When the user presses the home button, your application delegate's applicationWillResignActive is called which disables touch events to the application. Then applicationDidEnterBackground is called, which, per the Apple docs:
Your delegate’s applicationDidEnterBackground: method has
approximately 5 seconds to finish any tasks and return. In practice,
this method should return as quickly as possible. If the method does
not return before time runs out, your application is killed and purged
from memory
You need proof to show your boss that obviously isn't an iOS developer.
Apple Human Interface Guide
That should be all the proof you need. But to be clear, Apple will not allow an app to override the home button in any way. You can surely put action sheets or pop ups to warn before logging out, but once the home button is pressed, you are on notice to give up your memory, you are being shut down.
You might want to look into the Store Demo Mode of IOS. This way you can disbale the Home button and lock the device in the first app you start after booting.
I know I'm too late to answer this question.
But I recently came with the issue which Samssonart had.
The answer given by #iandotkelly is deprecated with iOS5. Now none of delegate method will be used to distinguish between locking the device or sending app to background using Home button.
you can now use applicationState variable to define what action is triggered.
applicationState is an inbuilt id provided by appDelegate.
**
if it returns 2 then, it will identify the Home button is pressed
if it returns 1 then, it will identify the lock hardware button is pressed
**
So, in your case you can check out this condition in **applicationDidEnterBackground** method
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(#"decision >> %d",[[UIApplication sharedApplication] applicationState]);
}
Enjoy Programming!
The best reference I can find is this one. It's not quite explicit, but Apple's Human Interface Guidelines have a couple of headings 'Always Be Prepared to Stop', followed by 'Don't Quite Programmatically', which spell out what the home button does and that you shouldn't be implementing your own quitting strategies.
I know this is an old topic, but I just want to update this answer. In iOS 7 this is not working.
So I use screenbrightness when the app will go to the background to identify difference between the Home and Lock button.
-(void)applicationDidEnterBackground:(UIApplication *)application {
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateInactive) {
NSLog(#"Sleep button pressed");
} else if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) {
if ([[UIScreen mainScreen] brightness] > 0.0)
NSLog(#"Home button pressed");
else
NSLog(#"Sleep button pressed");
}
}
I hope this is gonna be of any help for in future for anyone

How to ignore a local notification when iPhone is locked?

I am implementing a voip app for iphone. Upon receiving an incoming call, the app shows a local notification with two buttons : close and answer. If the user clicks on answer, the app shows in foreground and the call is answered, and if she clicks on 'close', the call is ignored.
The problem, however, is that when iphone is locked, there is no 'close' button, only a slider for which sliding from left to right means 'answer'. Hence there is no way to ignore the call.
Is there a way to solve this? The only solution I found so far is to show another notification for the user to answer or reject, but that seems inconvenient to use.
Re-locking the screen (using the sleep/lock button) usually constitutes ignoring a notification. Have you tested whether your app receives some kind of message when that happens?