Alexander Vasenin
Alexander Vasenin

Reputation: 12953

Type 'UIApplication' has no member 'didBecomeActiveNotification'

I'm trying to add observer for UIApplication.didBecomeActiveNotification with following code:

NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: nil) { _ in /* some code */ }

but Xcode says Type 'UIApplication' has no member 'didBecomeActiveNotification' despite it's officially documented as UIApplication class constant. What I'm doing wrong?

Upvotes: 4

Views: 11138

Answers (2)

Daniel T
Daniel T

Reputation: 311

When you are using Xcode 10, in Build Settings, if you set Swift Language Version to be Swift 4, you should write:

NotificationCenter.default.addObserver(forName: .UIApplicationDidBecomeActive, object: nil, queue: nil) { _ in /* some code */ }

If it is set to be Swift 4.2, use this instead:

NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: nil) { _ in /* some code */ }

Upvotes: 14

Alexander Vasenin
Alexander Vasenin

Reputation: 12953

You should change notification name to .UIApplicationDidBecomeActive like this:

NotificationCenter.default.addObserver(forName: .UIApplicationDidBecomeActive, object: nil, queue: nil) { _ in /* some code */ }

Apparently, didBecomeActiveNotification works for iOS 12 SDK only.

Upvotes: 4

Related Questions