Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

applicationWillResignActive called without reason on iOS 10 ( swift 3 )

Tags:

ios

ios10

swift

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. 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.

1 - It only occurs when the app starts in landscape mode
2 - It only occurs on iPhones and not on iPads
3 - The problem does NOT occur on an iOS 9 device

Did anyone noticed this problem ?

like image 725
Fox5150 Avatar asked Sep 21 '16 16:09

Fox5150


1 Answers

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 variable in AppDelegate which increments each time some system alert shows and decrements 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)

like image 84
abagmut Avatar answered Nov 03 '22 17:11

abagmut