How to broadcast messages in iOS and not put off Bob
There are multiple ways to communicate between components in the iOS world. You can pass parameters by constructor, segue, delegate pattern that is commonly used, or to choose NotificationCenter.
All these methods have a 1 on 1 relationship with exception of NotificationCenter
, it is 1 to many. NotificationCenter
is a really good tool to communicate between classes and structures, if it is used properly.
This article will explain how to:
- Observe simple Notification Posted by OS
- Observe Custom Notification created by us
- Observe Custom Notification what contains some Data
- How to avoid potential issues and crashes
Basics
On a high level it is broadcasting messages. The listeners / observer can receive these messages and do some actions or process them if they carry some information.
Imagine it, as when you subscribe to receive some newspaper, example from Better Programming.
Your 🐶 dog 🐾 will watch the mailbox for newspaper that is the Observer part.
When Better Programming sends out the newspaper, that is the Post part. Then you will receive from the Postman the newspaper in the mailbox.
But your neighbour, friend also could subscribe and receive it.
If you leave your address, and let’s say Bob moves in. And if you don’t remove the subscription, a new newspaper will still arrive. This is the Remove Observer part, you must remove yourself from the list who will be visited by the Postman.
Bob is a non technical person, who wants to understand everything. Bob maybe don’t understand the newspaper content and this will make him crazy. This is the possible crash part what will be explained latter.

The picture above represents the case where two ViewControllers
and Service
(green) subscribe and observe for the message, the NotificationCenter
, which can be posted from another ViewController
(red).

Don’t be confused in case you find the name NSNotificationCenter. It is practically the same, just Apple renamed it and did some small rework when switching happened from obj-c to swift. Calls and the functionalities are mostly the same with slight changes.
As we mentioned previously there are 2 types of notifications, user created Custom Notification and OS provided Notifications.
Custom Notifications
The code below represents the Observer part. As you can see every Notification has a name and method that will be called. Name should be uniq. It is the identifier to be able to determine if the message is posted for you.
You can see “@objc” as a method prefix. This means that under the hood we are using some Objective-c magic. The purpose of this is just to be able to recognise the method by NotificationCenter.
Next is how we broadcast / post the notification. It can also send data with theuserInfo
parameter. It could contain [AnyHashable: Any]?
data. This means we can pass anything.
Don’t make Bob crazy
Last but not least important is to remove the observer. If you remember, Bob can get crazy if he receives a message that he doesn’t understand.
If we don’t remove the observer, the memory address will still receive some information and this could result in strange behaviour and even crash what is hard to debug.
Remove All Observers
You can remove the Observer just by Name.
Remove Observer by Name
The detailed code is on this link Code Example.
OS provided Notifications
The OS provided Notification are like UIApplication.didBecomeActiveNotification
what is equivalent with AppDelegates
applicationDidBecomeActive(_:)
method or in case of SceneDelegate
with sceneDidBecomeActive(_:)
, only exception is that now you can observer for changes from any class or structure.
The difference between Custom Notification and OS provided, that OS Provided are posted by OS and Custom ones by us.
Useful Notifications are like:UIResponder.keyboardWillShowNotification
,UIResponder.keyboardWillHideNotification
,UIApplication.didBecomeActiveNotification
,NSManagedObjectContextDidSave
,
etc.
postsFrameChangedNotifications
Name it, probably Notification exists. Below is the full list from Apple.
Apple Developer Documentation
As you can see lots of them have prefixes like UIResponder
, UIApplication
etc. This means they belong to some Modules.
Examples
If we run the App with code what is in the link Code Example. Right away the next log will pop up. This is the way some Service
will observe Application Lifecycle changes.
In this case we have created a property of the Service
, what called the code to attach and listen for didBecomeActiveNotification
and right away as App has been started the attached method to the Notification was called.

Clicking on the provided example button we can see that NotificationCenter
posts are really 1 to Many. All observers will be notified. Also you can see that we also sent some data with the Notification. Receiver can handle the receiving data, but it is not necessary. It can listen just for the event, like in the example the AppDelegate
for the tap.

Post the Data:
How looks to Post Data with Notification
Receive and parse the Data:
How to Receive and parse the Data
Tips
If you want to be able to nicely group your Custom Notification, the code in the next is the solution. It also has an option if you want to expose notification to Obj-c.
Extension of Notification.Name
In case you are working in legacy code, here is the Objective-c counterpart.
Obj-c example
Sum up
- Notifications are a good way to broadcast messages when you need to send to multiple classes.
- They are a good way if you also need to listen to multiple Notifications in one class.
- When we need to listen for some OS specified changes.
- Don’t forget to remove Observers.
If you got to this point, thanks for reading. 🙂 If you like the content please 👏, share, subscribe, buy a coffee it means to me. If you have some suggestions or questions please feel free to comment.