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 ?