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
17
votes
1 answer

IOS iMessage extension screenshot detection

Please use Swift 4+ NOTE: I am detecting the screenshot while I am within the iMessage extension, not in the standard iMessage view. Update - I came up with a working solution that checks the photo library during the sensitive information period…
Levi K
  • 573
  • 1
  • 4
  • 23
17
votes
3 answers

Is removing a NotificationCenter observer that was created with closure syntax by name adequate?

I have a few notifications that were created using block / trailing closure syntax which look like this: NotificationCenter.default.addObserver(forName: .NSManagedObjectContextObjectsDidChange, object: moc, queue: nil) { note in //…
Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
16
votes
5 answers

No EAAccessoryDidConnectNotification and EAAccessoryDidDisconnectNotification after connecting Lightning kBD or Smart KBD

I could not get these two notifications EAAccessoryDidConnectNotification and EAAccessoryDidDisconnectNotification after connecting Lightning kBD or Smart KBD with my APP. But it worked on iOS9.3 with my code as below, [[NSNotificationCenter…
Ting
  • 169
  • 1
  • 4
16
votes
5 answers

Instance was deallocated while key value observers were still registered with it

I've got a UITableView. Here I got different cell's. Each cell has a model. With KVO and NotificationCenter the cell listen to the model for changes. When I leave the ViewController I get this error: An instance 0x109564200 of class Model was…
user89862
16
votes
4 answers

In ARC do we need to send removeObserver: explicitly?

Do we have to send removeObserver: explicitly for objects that have been added as observers to an NSNotificationCenter before? I am bit confused and unable to find the exact answer for this. Please provide me in detail, about this including why we…
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
15
votes
7 answers

NSNotification not being sent when postNotificationName: called

I'm trying to get one instance of using NSNotificationCenter with addObserver and postNotificationName but I can't work out why it won't work. I have 2 lines to code to add the observer and send the message in 2 different…
Affian
  • 3,380
  • 5
  • 22
  • 26
15
votes
2 answers

Is NSNotificationCenter thread safe?

Can I post a notification in a given queue and receive it on another? I want to use notifications to communicate different queues, but I'm not sure if this is safe...
14
votes
3 answers

Swift 4 - Notification Center addObserver issue

I'm crashing and getting an unrecognized selector error every time a Notification arrives and the App tries to execute its associated method. Here's my code - which is in viewDidLoad: let notificationCenter =…
Sirab33
  • 1,247
  • 3
  • 12
  • 27
14
votes
3 answers

Xcode debugger (lldb) get object description from memory address

Little-known fact: It is now possible, in Xcode, while paused in the debugger, to introspect the notification center to learn what objects are registered to receive what notifications: (lldb) po…
matt
  • 515,959
  • 87
  • 875
  • 1,141
14
votes
1 answer

iphone notification results in "unrecognized selector sent to instance..."

To make it short, I'm registering the following NSNotification listener in ClassA (in viewDidLoad): [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playSong) name:@"playNotification" object:nil]; I have the selector…
esbenr
  • 1,356
  • 1
  • 11
  • 34
14
votes
4 answers

Swift 3 - How to use enum raw value as NSNotification.Name?

I'm using Xcode 8 beta 5 and I'm trying to setup an enum of notifications like this enum Notes: String { case note1 case note2 } Then trying to use them as the notification names NotificationCenter.default.post(name: Notes.note1.rawValue as…
D. Greg
  • 991
  • 1
  • 9
  • 21
14
votes
2 answers

How do I debug this error: CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER?

I am getting an error in my app that I have no idea of how to figure out, has anyone seen this before: # Binary Image Name Address Symbol 0 RepZio 0x002bd302 testflight_backtrace 1 RepZio 0x002bc996 TFSignalHandler 2 …
Slee
  • 27,498
  • 52
  • 145
  • 243
14
votes
7 answers

NSNotificationCenter vs delegation - which is faster?

I have read a lot about the pros and cons of each, and I know delegates are usually for one listener, and notifications are for many. The question is about performance. I have read NSNotificationCenter vs delegation( using protocols )? I am sending…
13
votes
2 answers

Why doesn't Remove Observer from NSNotificationCenter:addObserverForName:usingBlock get called

I'm confused on why the observer is never removed in the following code. In my viewDidAppear I have the following: -(void)viewDidAppear:(BOOL)animated{ id gpsObserver = [[NSNotificationCenter defaultCenter] …
13
votes
2 answers

is it ok to use of a notification to communication back to the main thread of an IOS app? (cf performSelectorOnMainThread)

Is it ok to use of a notification to communication back to the main thread of an IOS app? (cf performSelectorOnMainThread). That is, there are are there any gottcha's for this purpose? Background want to call back to main UI thread from a…
Greg
  • 34,042
  • 79
  • 253
  • 454