Fox5150 Fox5150 - 2 months ago 181
Swift Question

applicationWillResignActive called without reason on iOS 10 ( swift 3 )

When I launch my app on iOS 10, I can see that after a short delay, the Appdelegate function -> applicationWillResignActive() is called.
There is no reason for that. The app is still active and in foreground state when it occurs and the app continues to run normally.

Please see above the lifecycle of my app :

--> Click on the app icon


  • App launch

  • application --> didFinishLaunchingWithOptions

  • application --> applicationDidBecomeActive

  • RootViewController --> viewDidAppear

  • application --> applicationWillResignActive <-- issue !

  • application --> applicationDidBecomeActive <-- again ??!!

  • at this point, the app is still running with no error



This sequence is repeated each time I open the app.

It looks as if something forces my app to quit the foreground state for an ultra short delay.
Another strange thing that I noticed : it only occurs when the app starts in landscape mode !
Usually, applicationDidBecomeActive is called when the app displays an alert ( for example if the app requires an user's permission to access the camera ) or when the user clicks on the home button.
The problem does NOT occur on an iOS 9 device.
Did anyone noticed this problem ?

Answer

The problem is, it calls second time after dismissing system services alert (location, push notifications, photos)

So the only way to handle it is to use varibale in AppDelegate which incremetns each time some system alert shows and decremants in applicationDidBecomeActive, so you call your code only if value of this variable is 1.

Another interesting thing is that applicationDidEnterBackground doesn't call when system alert shows, thus we can use this info to decide whether we should call our code in applicationDidBecomeActive or not (but still, it can be less reliable solution)