Questions tagged [nsnotificationcenter]

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

nsnotificationcenter
Filter by
Sorted by
Tagged with
633 votes
6 answers
415k views

Send and receive messages through NSNotificationCenter in Objective-C?

I am attempting to send and receive messages through NSNotificationCenter in Objective-C. However, I haven't been able to find any examples on how to do this. How do you send and receive messages ...
user avatar
462 votes
17 answers
426k views

NSNotificationCenter addObserver in Swift

How do you add an observer in Swift to the default notification center? I'm trying to port this line of code that sends a notification when the battery level changes. [[NSNotificationCenter ...
Berry Blue's user avatar
  • 15.8k
348 votes
44 answers
294k views

Move view with keyboard using Swift

I have an app that has a text field on the lower half of the view. This means that when I go to type in the text field the keyboard covers the textfield. How would I go about moving the view upwards ...
Alex Catchpole's user avatar
169 votes
7 answers
178k views

How to pass data using NotificationCenter in swift 3.0 and NSNotificationCenter in swift 2.0?

I'm implementing socket.io in my swift ios app. Currently on several panels I'm listening to the server and wait for incoming messages. I'm doing so by calling the getChatMessage function in each ...
user3766930's user avatar
  • 5,749
137 votes
5 answers
124k views

How to pass object with NSNotificationCenter

I am trying to pass an object from my app delegate to a notification receiver in another class. I want to pass integer messageTotal. Right now I have: In Receiver: - (void) receiveTestNotification:...
Jon's user avatar
  • 4,732
134 votes
13 answers
57k views

How do you create custom notifications in Swift 3?

In Objective-C, a custom notification is just a plain NSString, but it's not obvious in the WWDC version of Swift 3 just what it should be.
hexdreamer's user avatar
  • 1,789
107 votes
9 answers
29k views

Android equivalent to NSNotificationCenter

In the process of porting an iPhone application over to android, I am looking for the best way to communicate within the app. Intents seem to be the way to go, is this the best (only) option? ...
John's user avatar
  • 1,528
85 votes
8 answers
22k views

iOS 11 - Keyboard Height is returning 0 in keyboard notification

I have been using Keyboard notifications without any problem and getting exact height of Keyboard. - (void)keyboardDidShow:(NSNotification *) notification{ CGSize keyboardSize = [[[notification ...
Hassy's user avatar
  • 5,158
78 votes
4 answers
39k views

Posting NSNotification on the main thread

I found the following code snippet which allows NSNotification to be posted on the main thread from any background thread. I would like to know if this is a safe and acceptable practice please? ...
RunLoop's user avatar
  • 20.5k
74 votes
2 answers
66k views

how to use the object property of NSNotificationcenter

Could somebody please show me how to use the object property on NSNotifcationCenter. I want to be able to use it to pass an integer value to my selector method. This is how I have set up the ...
dubbeat's user avatar
  • 7,788
69 votes
6 answers
35k views

How to set addObserver in SwiftUI?

How do I add NotificationCenter.default.addObserve in SwiftUI? When I tried adding observer I get below error Argument of '#selector' refers to instance method 'VPNDidChangeStatus' that is not ...
O-mkar's user avatar
  • 5,558
68 votes
6 answers
21k views

Post of NSNotificationCenter causing "EXC_BAD_ACCESS" exception

A UIViewController adds itself to the default center: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editFood) name:@"editFood" object:nil]; Then a UITableView ...
Paul Jordan's user avatar
64 votes
4 answers
26k views

OS X: Detect system-wide keyDown events?

I'm working on a typing-tutor application for Mac OS X that needs to have keystrokes forwarded to it, even when the application is not in focus. Is there a way to have the system forward keystrokes ...
Chris Ladd's user avatar
  • 2,805
51 votes
9 answers
42k views

How to receive NSNotifications from UIWebView embedded YouTube video playback

I didn't received any notifications for MPMoviePlayerController. What am I doing wrong? I use following logic. I'm begining to play youtube video in UIWebView. UIWebView calls a standard ...
Voloda2's user avatar
  • 12.5k
49 votes
1 answer
18k views

How can I listen for all notifications sent to the iOS NSNotificationCenter's defaultCenter?

I want to listen to all notifications dispatched to the defaultCenter. Both public and private. Does anyone know how I can do this?
Tony's user avatar
  • 18.9k
45 votes
11 answers
76k views

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 ...
iamktothed's user avatar
  • 1,428
42 votes
1 answer
10k views

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 (...
Munna89's user avatar
  • 623
39 votes
3 answers
44k views

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 ...
user278859's user avatar
  • 10.5k
38 votes
7 answers
40k views

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 ...
user3115014's user avatar
38 votes
7 answers
18k views

NSNotificationCenter : list of observers?

Is it possible to get the list of observers (objects and selectors) for a given notification name? (NSNotificationCenter)
Antoine Rosset's user avatar
34 votes
2 answers
47k views

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 ...
MichiZH's user avatar
  • 5,707
32 votes
4 answers
13k views

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 ...
Ludovic Landry's user avatar
30 votes
5 answers
28k views

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" ...
BarryK88's user avatar
  • 1,806
29 votes
6 answers
36k views

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....
Krunal's user avatar
  • 78.5k
29 votes
3 answers
16k views

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 ...
tranvutuan's user avatar
  • 6,109
28 votes
2 answers
25k views

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 ...
4thSpace's user avatar
  • 44k
28 votes
3 answers
3k views

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 ...
Jalol Erkiniy's user avatar
26 votes
3 answers
18k views

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. ...
user2413621's user avatar
  • 2,916
26 votes
6 answers
4k views

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 ...
E. Rivera's user avatar
  • 10.9k
25 votes
12 answers
50k views

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) ...
ICL1901's user avatar
  • 7,738
25 votes
9 answers
12k views

OSX Notification Center Icon

I'm using OSX's Notification Center APIs for the first time and can't seem to figure out how to make my app's icon to show up in the Notification badge. The default "your app doesn't have an icon" ...
phatduckk's user avatar
  • 1,785
23 votes
3 answers
21k views

How to post and receive an NSNotifications (Objective C) | Notifications (in Swift)?

Is there an easy-to-grock pattern how to send a NSNotification (Objective C) | Notification (in Swift) and how to receive one? Code snippet? The docs write like 150 pages on the topic. Would like to ...
dontWatchMyProfile's user avatar
23 votes
7 answers
13k views

thumbnailImageAtTime: now deprecated - What's the alternative?

Until iOS7 update I was using... UIImage *image = [moviePlayer thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame]; ...with great success, so that my app could show a still of the ...
Jim Tierney's user avatar
  • 4,094
23 votes
3 answers
4k views

NSNotification order of observer notifications

If I have several classes observing a particular NSNotification, in what order are the observers notified when the notification is posted?
whatdoesitallmean's user avatar
23 votes
3 answers
5k views

When to create a custom NSNotificationCenter?

I've been playing about with the NSNotificationCenter and I've been wondering when you would use your own custom notification center rather than the defaultCenter? And what would the advantages of ...
Jimmery's user avatar
  • 9,963
22 votes
8 answers
26k views

How do I check if an UIViewController is currently being displayed?

How do I check if an UIViewController is currently being displayed? My UIViewControllers are listening for NSNotifications - even if when they are not displayed (ie not shown). So I could have 10 ...
ikevin8me's user avatar
  • 4,293
21 votes
3 answers
16k views

Removing a NSNotificationCenter observer in iOS 5 ARC

I have an iOS 5 ARC-based project, and am having difficulty about where I should be removing the observer for the NSNotificationCenter observations which I have registered within a UIViewController. ...
Skoota's user avatar
  • 5,300
21 votes
3 answers
4k views

Warning for iOS/iPhone users about duplicate NSNotification observations

This isn't a question so much as a warning to others to save them some time. NSNotificationCenter on iOS 3/iPhone OS 3 (I'm assuming also Mac OS X and iOS 4) has the following behavior: If you ...
God of Biscuits's user avatar
20 votes
10 answers
47k views

NSNotificationCenter Swift 3.0 on keyboard show and hide

I am trying to run a function when the keyboard shows and disappears and have the following code: let notificationCenter = NotificationCenter.default notificationCenter.addObserver(self, selector: #...
Dhwanit Zaveri's user avatar
20 votes
3 answers
15k views

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

With Swift 4.2, getting following error, that was working fine with Swift 4. Type 'NSNotification.Name' has no member 'UITextField' Here is my error code. NotificationCenter.default.addObserver(...
Krunal's user avatar
  • 78.5k
20 votes
3 answers
4k views

NSUserNotificationCenter dismiss notification

I'm trying to use the new Mountain Lion NSUserNotificationCenter for my application (which isn't too hard actually). Posting notifications works like a charm via NSUserNotification *userNotification ...
BinaryBucks's user avatar
19 votes
4 answers
19k views

NSOperation and NSNotificationCenter on the main thread

I have an NSOperation. When it is finished I fire a NSNotificationCenter to let the program know that the NSoperation is finished and to update the gui. To my understanding listeners to the ...
Mel's user avatar
  • 2,065
19 votes
4 answers
26k views

Detect rotation changes in iOS

I am making an iOS app that needs to do a little interface rearrangement upon rotation. I am trying to detect this by implementing - (void)orientationChanged:(NSNotification *)note, but this gives me ...
Undo's user avatar
  • 25.6k
19 votes
5 answers
22k views

Adding and removing observers to NSNotificationCenter in a UIViewController

Looking at various Apple examples (for example Add Music) in which I see they add observers to the default NSNotificationCenter in viewDidLoad, then remove them in dealloc. This seems dangerous as ...
Undistraction's user avatar
19 votes
2 answers
17k views

Observing Changes to a mutable array using KVO vs. NSNotificationCenter

In my model I have an array of objects called events. I would like my controller to be notified whenever a new object is added to events. I thought that a good way to do this would be use the KVO ...
MathewS's user avatar
  • 2,287
18 votes
3 answers
12k views

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, ...
Dan Beaulieu's user avatar
  • 19.7k
17 votes
1 answer
556 views

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 ...
Levi K's user avatar
  • 583
16 votes
4 answers
4k views

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 ...
Anoop Vaidya's user avatar
  • 46.4k
16 votes
5 answers
25k views

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: ...
user avatar
16 votes
5 answers
3k views

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 ...
Ting's user avatar
  • 169

1
2 3 4 5
32