5

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?

Samssonart
  • 3,443
  • 3
  • 31
  • 41

8 Answers8

16

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.

iandotkelly
  • 9,024
  • 8
  • 48
  • 67
  • I didn't quote the App Store guidelines directly, as its a copyrighted document, only accessible if you have a developer account. However if you're being asked to develop iOS applications, then you must have access. I suggest you have a look at it. – iandotkelly Aug 18 '11 at 19:25
2

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:

  1. 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).
  2. 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.
  3. 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...

ima747
  • 4,667
  • 3
  • 36
  • 46
2

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

highlycaffeinated
  • 19,729
  • 9
  • 60
  • 91
1

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!

Niru Mukund Shah
  • 4,637
  • 2
  • 20
  • 34
  • 1
    I am tempted to vote down on this: it is not an answer to the original question. It should be a comment on iandotkelly's answer. – vikingosegundo Aug 30 '13 at 13:02
  • Yes, I do know this is not an answer to an original question. But I just want to inform you guys that the method in the accepted answer is no longer useful now. This is something you can check after coming back to your app rather checking condition while closing it. And as far as comment is concerned, I don't know how to arrange words in comment like we arrange in answer. That's why I prefer to give answer and let everybody know about the method. – Niru Mukund Shah Aug 31 '13 at 05:05
1

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.

Bill Burgess
  • 14,054
  • 6
  • 49
  • 86
1

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.

user1109729
  • 121
  • 1
  • 3
0

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

0

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.

Tommy
  • 99,986
  • 12
  • 185
  • 204