Hope
Hope

Reputation: 2326

In a Swift application why WatchKit and NSNotificationCenter is working one way only?

When a NSNotificationCenter notification send from WatchKit Extension InterfaceController we can get it from ViewController but when notification send from ViewController we can not get it from WatchKit Extension InterfaceController.

Here is the working notification;

We set notification in ViewController as

   override func viewDidLoad() {
    super.viewDidLoad()

  NSNotificationCenter.defaultCenter().addObserver(self, selector:    "action_notification_WatchButton:", name: "notification_WatchButton", object: nil) 

 }

We get notification from in ViewController with

 func action_notification_WatchButton(notification:NSNotification) {

    println("watch button touched")        
}

We send notification from WatchKit Extension InterfaceController as

     @IBAction func buttonWatch() {

     NSNotificationCenter.defaultCenter().postNotificationName("notification_WatchButton", object:nil, userInfo:nil)

}

So that works while both watch and phone applications are running. When we touch button watch

println("watch button touched")

prints the text. Very nice.

But when we set notification in

override func willActivate() {

  NSNotificationCenter.defaultCenter().addObserver(self, selector:    "action_notification_WatchButton:", name: "notification_WatchButton", object: nil)  

 }

and we put

  func action_notification_WatchButton(notification:NSNotification) {

    println("phone button touched")        
}

under InterfaceController at WatchKit Extension and we use

@IBAction func buttonPhone() {
     NSNotificationCenter.defaultCenter().postNotificationName("notification_WatchButton", object:nil, userInfo:nil)
  }

inside of ViewController button function there is no answer as the button touched on Phone.

Upvotes: 2

Views: 1709

Answers (1)

cnoon
cnoon

Reputation: 16663

You should read up on Darwin notifications and MMWormhole. That will solve your notification issues.

Upvotes: 3

Related Questions