57

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()

        }
    }
}
Rakesha Shastri
  • 11,053
  • 3
  • 37
  • 50
mathias merlot
  • 585
  • 1
  • 4
  • 4

5 Answers5

126

Change

forName: Notification.Name.UIApplicationDidEnterBackground

to

forName: UIApplication.didEnterBackgroundNotification
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 3
    it says: 'didEnterBackgroundNotification' has been renamed to 'NSNotification.Name.UIApplicationDidEnterBackground' –  Jan 27 '19 at 23:10
5

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)
Deviyani Swami
  • 749
  • 8
  • 17
3

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

HeyImChris
  • 31
  • 1
3

Xcode 11 , swift 5

UIApplication.didBecomeActiveNotification
Ravikanth
  • 308
  • 1
  • 10
2

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
adrapp
  • 47
  • 6