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

5

58

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

        }
    }
}
Odense answered 22/7, 2018 at 18:36 Comment(1)
It doesn't work, still have the same errorOdense
A
128

Change

forName: Notification.Name.UIApplicationDidEnterBackground

to

forName: UIApplication.didEnterBackgroundNotification
Annettaannette answered 23/7, 2018 at 0:2 Comment(1)
it says: 'didEnterBackgroundNotification' has been renamed to 'NSNotification.Name.UIApplicationDidEnterBackground'Doubletalk
A
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)
Ashla answered 13/2, 2019 at 7:42 Comment(0)
A
4

Xcode 11 , swift 5

UIApplication.didBecomeActiveNotification
Aviv answered 24/9, 2019 at 9:48 Comment(1)
You forgot import UIKitKhz
M
3

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

Miry answered 21/6, 2019 at 0:8 Comment(0)
H
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
Hydroelectric answered 24/5, 2020 at 18:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.