how to add observer for a variable in AppDelegate to other controllers?

Hi.

I initialized a variable for connection status in appDelegate and I run the observe of connection but I want to add observer in all viewConttrollers , because I want to show label to indicate the connection status depending on value of my global variable in appDelegate.

how can I doing this?

thanx.

What is the problem ?


The simplest is to add directly in viewDidLoad

- notification (kNotification) is defined in

extension  Notification.Name {
    public static let kMyNotification = Notification.Name("myNotification")
}

- in viewDidLoad

      NotificationCenter.default.addObserver(self, selector: #selector(handleMyNotification(_:)), name: .kMyNotification, object: nil)

- then define the func

    @objc func handleMyNotification(_ sender: Notification) {

}


You could probably implement in extensions, but the first solution is simpler

https://stackoverflow.com/questions/33022540/swift-make-protocol-extension-a-notification-observer

my proplem is.. I initialized a variable handle connection state by observer.

i want to observe the changing in value of this variable in viewControllers.


my variable in appDelegate is named "connectivity":


import UIKit
import Connectivity

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    let connectivity: Connectivity = Connectivity()
}


where I put "connectivity" in your code you have pasted above?

You do not use connectivity in controllers.


But when connectivity changes (it is up to you to know where), you will post a notification (in appDelegate)

        let nc = NotificationCenter.default
        nc.post(name: .kMyNotification, object: nil)

Then, any windowController that is loaded will receive it.


Note: check if connectivity is a const or a var

unfortunately, I can't figure out your code, I just working with notifications for first time.

I want to observe value of connectivity in other "viewController"

I just need to configure your code with my variable.

how to do that.please

You want to observe the connectivity value, automatically when it changes ? Or do you want to do it on request ?


To observe when it changes

You need first to define the identifier for the notification.


This is done at global level, outside any class dedinition :

extension  Notification.Name {
    public static let kMyNotification = Notification.Name("myNotification")
}


Where does connectivity value changes ?

Each time it changes, post a notification by inserting the following just after the change:

        let nc = NotificationCenter.default
        nc.post(name: .kMyNotification, object: nil)


In each viewController where you want to receive the notification, subscribe to it in its viewDidLoad:

    NotificationCenter.default.addObserver(self, selector: #selector(handleMyNotification(_:)), name: .kMyNotification, object: nil)


And define in each viewController what you do when notification received, by adding the following function:

    @objc func handleMyNotification(_ sender: Notification) { 
      // Do what you need when receiving notification, like update a label…
     print("Just to check I received a notification")
}


To observe on request,

- you can use delegation



Is that clear now ?

how to add observer for a variable in AppDelegate to other controllers?
 
 
Q