How to close the iPhone app when the screen locks?

I am writing an application that includes authentication on the home screen and allows you to access sensitive data on later screens. When the iPhone is locked, either using the lock button or using the automatic lock, I would like the application to close as a security measure. Is there a way I can do this?

+7
source share
4 answers

Your UIApplicationDelegate will receive

– applicationWillResignActive: 

when the screen is locked, and

 – applicationDidBecomeActive: 

when he will return. However, it can also receive these messages in other situations (for example, when receiving a phone call, the user closes the application on iOS 4.0 and later), and I do not know how to recognize the cause.

The best user interface, in my opinion, would be to re-authenticate when the application returns. Thus, the user will not be confused when the phone is unlocked, and the application that they had started mysteriously stopped.

+10
source

To eliminate any confusion, I just ran a few tests on a device with iOS 4.3.2:

When you start the application, your application is sent: application: didFinishLaunchingWithOptions: applicationDidBecomeActive:

When you press the home button, your application will be sent:

 applicationWillResignActive: applicationDidEnterBackground: 

When you restart this application, the application is sent:

 applicationWillEnterForeground: applicationDidBecomeActive: 

When you press the lock button, your application will be sent:

 applicationWillResignActive: 

When you unlock, your application is sent:

 applicationDidBecomeActive: 

When you receive a call, your application is sent:

 applicationWillResignActive: 

If you do not answer this call, your application will be sent:

 applicationDidBecomeActive: 

When you receive a call, your application is sent:

 applicationWillResignActive: 

If you answer this call, your application will be sent:

 applicationDidEnterBackground: 

When you hang up, your application is sent:

 applicationWillEnterForeground: applicationDidBecomeActive: 

I would say when you receive applicationWillResignActive : then you must log out, cancel authentication, block or cancel your confidential information, and when you receive applicationDidBecomeActive: then re-authenticate. This call is called at startup, returns from the background and unlocks the device.

In addition, these two methods may be of interest to you, but they really do not help the specific case that interests you:

 - (void)applicationProtectedDataWillBecomeUnavailable:(UIApplication *)application - (void)applicationProtectedDataDidBecomeAvailable:(UIApplication *)application 
+6
source
  • "Closing the application" is contrary to the recommendations of Apple. Although some applications actually do this, this is one of the things that can cancel your application. This was against Apple’s principles of multitasking and is now more important because of multitasking. The user interface is when they return to the phone and your application after blocking or are in another application, your application should still work. "Close the application" will make the user believe that the application has crashed, and they will probably write reviews about it.

  • If you want to protect information in the application when the user "leaves", you should look into the UIApplicationDelegate Protocol Reference . Particualrly applicationDidEnterBackground: (where you should logout the user) and applicationDidBecomeActive: (where you should record the user).

As a final note, you may need to default to “automatically log off”, but give the user a settings option so that they log in if they so wish. Not so difficult, and few who want it can use the setting.

+3
source

If I remember correctly, the application falls asleep when the phone is locked.

Since Apple's introduction of multitasking has changed the behavior so that your application will be sent a specific message when unlocking / switching to.

I would suggest just listening to this thing and then asking for authentication again.

I don’t think you can just exit your application (and shouldn’t), I don’t know a single application that just kills itself ...

Sorry for not being too specific here, but I hope you now find out where to go ...

+2
source

All Articles