Saturday, July 15, 2017

NSNotificationCenter is Communication tools internal to your app

In App Development some time we need internal communication to pass message to one ViewController to other ViewController. As Example. I am using Google login using Firebase now login task doing AppDelegate Now i need message when login success or fail on ViewController.  Then here we can use NSNotificationCenter.

1. Create Globally define a"Special Notification Key" constant that can be broadcast. on ViewController

   let mySpecialNotificationKey = "com.arkay.specialNotificationKey" 


2. Set Observer on ViewController.
        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.updateNotificationSentLabel), name: NSNotification.Name(rawValue: mySpecialNotificationKey), object: nil)

3. Create Func that can call when Notify ViewController

 func updateNotificationSentLabel(){
        btnSignIn.setTitle("Login Out",for: .normal)
    }]

4. Now Call When need. Right now i am calling from AppDelegate.

        NotificationCenter.default.post(name: Notification.Name(rawValue: mySpecialNotificationKey), object: self)


That's it.

No comments:

Post a Comment