iOS home button warning, is it possible?

2020-02-07 02:08发布

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?

标签: iphone ios ipad
8条回答
Root(大扎)
2楼-- · 2020-02-07 03:01

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!

查看更多
三岁会撩人
3楼-- · 2020-02-07 03:08

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

查看更多
登录 后发表回答