Can't post notification from AppDelegate

931 views ios
6

This is my code:

NotificationCenter.default.post(name: Notification.Name("isOnline"), object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(disableMarcar), name: Notification.Name("isOnline"), object: nil)

If I post the notification from any ViewController it works but from the AppDelegate.swift in func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { doesn't work.

Does anyone know why? I've seen other post but none answers my question.

answered question

1 Answer

12

Posting notifications before an observer is registerered does nothing, I'd consider that a feature. Do this instead:

NotificationCenter.default.addObserver(self, selector: #selector(disableMarcar), name: Notification.Name("isOnline"), object: nil)
NotificationCenter.default.post(name: Notification.Name("isOnline"), object: nil

posted this

Have an answer?

JD

Please login first before posting an answer.