Type 'Notification.Name' (aka 'NSNotification.Name') has no member 'UIApplication'

SwiftNotificationcenter

Swift Problem Overview


First it said that

> 'UIApplicationDidEnterBackground' has been renamed to > 'UIApplication.didEnterBackgroundNotification'

and when I dot it,it said

> Type 'Notification.Name' (aka 'NSNotification.Name') has no member > 'UIApplication'

func listenForBackgroundNotification() {
    observer = NotificationCenter.default.addObserver(forName: Notification.Name.UIApplicationDidEnterBackground, object: nil, queue: OperationQueue.main) { [weak self] _ in
        if let weakSelf = self {
            if weakSelf.presentedViewController != nil {
                weakSelf.dismiss(animated: true, completion: nil)
            }
            weakSelf.descriptionTextView.resignFirstResponder()
            
        }
    }
}

Swift Solutions


Solution 1 - Swift

Change

forName: Notification.Name.UIApplicationDidEnterBackground

to

forName: UIApplication.didEnterBackgroundNotification

Solution 2 - Swift

Error with Type 'NSNotification' has no member 'UIApplication' in swift4.2

NotificationCenter.default.addObserver(self, selector:#selector(handleNotification), name: NSNotification.Name.UIApplicationDidEnterBackground, object: nil)

Need to Change accordingly

NotificationCenter.default.addObserver(self, selector:#selector(handleNotification), name: UIApplication.didEnterBackgroundNotification, object: nil)

Solution 3 - Swift

If UIApplicaiton.didEnterBackgroundNotificaiton isn't working, try just .UIApplicationDidEnterBackground instead.

Solution 4 - Swift

Xcode 11 , swift 5

UIApplication.didBecomeActiveNotification

Solution 5 - Swift

Xcode 11.4.1, Swift 5

Had the exact same issue. My problem was I didn't import UIKit in my custom class

import UIKit

Then I was able to implement the following:

name: UIApplication.didEnterBackgroundNotification

Solution 6 - Swift

Similar error 'UIApplicationDidChangeStatusBarOrientation' has been renamed to 'UIApplication.didChangeStatusBarOrientationNotification'

To solve this error in Swift 5

You should change the way you call:

NotificationCenter.default.addObserver(self, selector: #selector(self.configureActivityIndicatorPosition), name: .UIApplicationDidChangeStatusBarOrientation, object: nil)

To

NotificationCenter.default.addObserver(self, selector: #selector(self.configureActivityIndicatorPosition), name: UIApplication.didChangeStatusBarOrientationNotification, object: nil)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmathias merlotView Question on Stackoverflow
Solution 1 - SwiftmattView Answer on Stackoverflow
Solution 2 - SwiftDeviyani SwamiView Answer on Stackoverflow
Solution 3 - SwiftHeyImChrisView Answer on Stackoverflow
Solution 4 - SwiftRavikanthView Answer on Stackoverflow
Solution 5 - SwiftadrappView Answer on Stackoverflow
Solution 6 - SwiftParesh MangukiyaView Answer on Stackoverflow