Anthony Wieser
Anthony Wieser

Reputation: 4611

Why does my UIApplicationDelegate receive applicationDidBecomeActive when pulling down notification center?

I've built a bare application with no functionality in XCode, and put logging statements in the applicationDidBecomeActive and applicationWillResignActive methods.

When I swipe down to show the notification center, I see the following:

2018-01-03 10:18:16.867028+0000 BareProject[1165:2053601] Resigning active

2018-01-03 10:18:17.510713+0000 BareProject[1165:2053601] Active

2018-01-03 10:18:17.634805+0000 BareProject[1165:2053601] Resigning active

Is this intended? My code does quite a lot of work when becoming active, only to have the rug pulled out from it again about 120ms later, and it seems that the documentation says I should be using applicationDidBecomeActive to restart tasks: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622956-applicationdidbecomeactive?language=objc

I tried this on ios 10.3 and this behavior does not exist.

Upvotes: 17

Views: 1900

Answers (2)

Peter Lapisu
Peter Lapisu

Reputation: 20965

looks like this is a bug, that made it to regular behavior

you need to look for applicationDidEnterBackground

however, in case you need to handle your code for this you can use the following to NOT run code in applicationDidBecomeActive when the notification center swipes down

class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var didEnterBackground = true
    
    func applicationDidBecomeActive(_ application: UIApplication) {
       if !didEnterBackground {
           return
       }
       didEnterBackground = false

       // run your code       
    }
    
    func applicationDidEnterBackground(_ application: UIApplication) {
        didEnterBackground = true
    }
    
}

Upvotes: 0

DevGansta
DevGansta

Reputation: 5844

There are actually two issues, the unexpected call to applicationDidBecomeActive: and the duplicated call to applicationWillResignActive:.

"Pull down" to show a notification center used to work properly on iOS 9. Only applicationWillResignActive: used to be called by the system, just verified it with iOS 9 Simulator.

On iOS 11.2.6, the applicationDidBecomeActive: gets called as you described which seems like an Apple bug. In this particular case, the system behavior conflicts with the documentation. Here is another example where the documentation deviates from the behavior you observed https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/StrategiesforHandlingAppStateTransitions/StrategiesforHandlingAppStateTransitions.html#//apple_ref/doc/uid/TP40007072-CH8-SW10

When your app is moved back to the active state, its applicationDidBecomeActive: method should reverse any of the steps taken in the applicationWillResignActive: method.

Upvotes: 11

Related Questions