Questions tagged [nsnotificationcenter]

NSNotificationCenter allows to send and register for notifications in the Foundation framework, which is provided by Apple.

An NSNotificationCenter object (or simply, notification center) provides a mechanism for broadcasting information within a program. An NSNotificationCenter object is essentially a notification dispatch table. Notifications are differentiated according to their name and contain an object (the sender) as well as any specific data.

Objects register with a notification center to receive notifications (NSNotification objects) using the addObserver:selector:name:object: or addObserverForName:object:queue:usingBlock: methods. Each invocation of this method specifies a set of notifications. Therefore, objects may register as observers of different notification sets by calling these methods several times.

You can also post notifications with postNotification:, or create and post notifications with postNotificationName:object: and postNotificationName:object:userInfo:, where you may use custom names for your notifications.

References:

1577 questions
44
votes
11 answers

Detecting Network Connectivity Changes using Reachability, NSNotification and Network Link Conditioner in Swift

From iOS 12 you simply use NWPathMonitor which is a line of code (example). For historic purposes: I'm trying to integrate network connectivity detection into my app, however it seems that somewhere along the line I have made a mistake as my…
iamktothed
  • 1,398
  • 2
  • 14
  • 22
42
votes
1 answer

Are NSNotificationCenter events received synchronously or asynchronously?

If a class registers for NSNotificationCenter events of a certain type and another class posts an event of that type, will the code in the receiver execute before (synchronously) or after (asynchronously) the posting class continues? - (void)poster…
Munna89
  • 613
  • 1
  • 6
  • 10
39
votes
3 answers

How to pass a NSDictionary with postNotificationName:object:

I am trying to pass an NSDictionary form a UIView to a UIViewController using NSNotificationCenter. The dictionary works fine at the time the notification is posted, but in the receiving method I am unable to access any of the objects in the…
user278859
  • 10,379
  • 12
  • 51
  • 74
37
votes
7 answers

NSNotificationCenter : list of observers?

Is it possible to get the list of observers (objects and selectors) for a given notification name? (NSNotificationCenter)
Antoine Rosset
  • 1,045
  • 2
  • 13
  • 22
36
votes
7 answers

iOS NSNotificationCenter to check whether the app came from background to foreground

I have a situation in which i have to intialize an object everytime when it comes from background to foreground and that should be using the NSNotificationCenter not with appdelegate because iam building a static library so there wont be appdelegate…
user3115014
  • 667
  • 1
  • 8
  • 23
34
votes
2 answers

iOS Remove observer from notification: Can I call this once for all observers? And even if there are none?

I'm registering three observers in most of my view controllers. Some have more, some less but I want to include part of the registration and unregistration process in a parent class. Is there any problem with calling the unregistering even if there…
MichiZH
  • 5,587
  • 12
  • 41
  • 81
31
votes
4 answers

How to communicate between iOS App Containing Extension and Extension (not Host App)

TLDR: Is it possible to send realtime messages or notifications between iOS App and it's Extension? I'm writing an iOS App with an extension that are part of the same App Group and share the same CoreData (SQLite database). I can read and write to…
Ludovic Landry
  • 11,606
  • 10
  • 48
  • 80
30
votes
5 answers

Why is my NSNotification its observer called multiple times?

Within an App I make use of several viewcontrollers. On one viewcontroller an observer is initialized as follows: [[NSNotificationCenter defaultCenter] removeObserver:self name:@"MyNotification" object:nil]; [[NSNotificationCenter defaultCenter]…
BarryK88
  • 1,806
  • 2
  • 25
  • 41
29
votes
6 answers

Type 'NSNotification.Name' has no member 'keyboardDidShowNotification'

I'm getting this error with Swift 4.2 Type 'NSNotification.Name' has no member 'keyboardDidShowNotification' Here is my code: NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidShow(notification:)), name:…
Krunal
  • 77,632
  • 48
  • 245
  • 261
29
votes
3 answers

What is parameter `object` in NSNotification addObserver:?

One of my class named Message.m is posting a notification with an object sentObject as below NSDictionary *sentObject = [NSDictionary dictionaryWithObjectsAndKeys:draftData.arr,@"data", nil]; //Post notification to inform a receiver to reload data …
tranvutuan
  • 6,089
  • 8
  • 47
  • 83
28
votes
3 answers

CNContactStoreDidChangeNotification is fired multiple times

I am able to observe the CNContactStoreDidChangeNotification when the contact database is changed while the app is in background state. I am pretty sure that only one observer was added to NSNotificationCenter. The problem is NSNotificationCenter…
27
votes
2 answers

Does removeObserver() remove all observers?

Does the following remove all NSNotificationCenter.defaultCenter there were added by name a view? NotificationCenter.default.removeObserver(self) If I have the following in the same view of viewDidLoad(), will they be removed with the single line…
4thSpace
  • 43,672
  • 97
  • 296
  • 475
27
votes
3 answers

How to write unit test for NSNotification

I am working in swift, I want to refresh a page so I am sending it using notification, I am posting a notification in one ViewController and adding observer in another and it is working perfectly. What I want to do is add unit test to it in swift. I…
user2413621
  • 2,916
  • 7
  • 24
  • 28
25
votes
12 answers

How to write Keyboard notifications in Swift 3

I'm trying to update this code to swift 3: NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self,…
ICL1901
  • 7,632
  • 14
  • 90
  • 138
25
votes
6 answers

NSManagedObject's hasChanges is true while changedValues is empty

I am trying to observe individual NSManagedObject changes on NSManagedObjectContextWillSaveNotification: - (void)managedObjectContextWillSave:(NSNotification *)notification { for (NSManagedObject * object in self.mutableObservedManagedObjects) …
Rivera
  • 10,792
  • 3
  • 58
  • 102