Widgets
- Swift Note 1 0 – Widget For Notifications Centers
- Swift Note 1 0 – Widget For Notifications Centered
- Swift Note 1 0 – Widget For Notifications Centerpieces
A widget elevates key content from your app and displays it where people can see it at a glance on iPhone, iPad, and Mac. Useful and delightful, widgets can also help people personalize their iPhone Home screens in unique ways.
Note: We do not provide support to upload or publish purchased applications in the App Store. Feel free to ask questions! Changelog: V.1.2 - Add Google AdMob & Interstitial banner V.1.0 - Initial release. App Extensions: Creating a Notification Center Widget June 12, 2015 As of iOS 8 your app can add a section to the Notification Center that users see when they swipe down from the top of the screen. This view is called a Today extension or just a widget. This tutorial has been updated for Swift 2.0. (Check out the 10 widgets that belong on your lock screen.) With all these widgets helping me out, Siri App Suggestions - the two rows of 'suggested' apps that appear on the lock screen - are. NotiWidget is a widget that let's you interact with your notifications on your lock screen, home screen, and DayDream screen saver. Main features: 1 - The notifications look exactly like the original ones. No custom, ugly appearance. Everything just looks as it should. 2 - Full control of all actions and media players. If the notification has a reply action or a play/pause action, you can use.
In iOS 14, iPadOS 14, and macOS 11, widgets differ from the Today widgets available in earlier versions, offering a redesigned appearance and expanded capabilities. Widgets also use a different implementation framework; for developer guidance, see Creating a Widget Extension.
A Closer Look at Widgets
You can create widgets in small, medium, or large sizes. On iPhone, iPad, and Mac, people find widgets in the widget gallery, where they can also choose a widget's size. After they choose a widget, people enter an edit mode where they can move the widget to their preferred location and in many cases, configure it. For example, people might choose to put multiple small Weather widgets on the Home screen so that they can configure each one to display the weather for a different location. People can place widgets on the Home screen or Today View on iPhone, the Today View on iPad, and in the macOS Notification Center.
On iPhone and iPad, the widget gallery also offers a prebuilt stack of widgets — called the Smart Stack — that people can place on their iPhone Home screen or Today View on iPhone or iPad. The Smart Stack contains a default set of widgets, including widgets from apps the user opens frequently. Over time, Siri can highlight specific widgets in the stack when they're most likely to be relevant in the current context. When people enable Smart Rotate, Siri can also highlight relevant widgets within custom stacks that people create.
NOTE Today widgets designed for iOS 13 and earlier aren't available on the Home screen, but remain accessible at the bottom of the Today View and in the macOS Notification Center.
Creating a Useful, Focused Widget
Although people can tap a widget to see or do more in your app, a widget's main purpose is to display a small amount of timely, personally relevant information that people can view without opening your app. Identifying a single idea for a widget and scoping the information to display are crucial first steps in the design process.
Focus your widget on one idea. Widgets are glanceable because they display only a small amount of information, so it generally works best to choose a simple idea that's clearly related to your app's main purpose. In some cases, this might mean choosing an idea that resembles the app's main purpose — for example, a weather app's widget could show the weather for a single location or the widget for a calorie-tracking app might show calories burned for the day. In other cases, a widget's idea can reflect an aspect of the app's main purpose — for example, a game's widget could show a character's status or a drawing app's widget might display favorite sketches.
In each size, display only the information that's directly related to the widget's idea. In larger widgets, you can display more data — or more detailed visualizations of the data — but it's crucial to stay focused on the widget's idea. For example, the small Weather widget shows only the current temperature and weather conditions, and the day's high and low temperature values for a location. The medium Weather widget shows the same data and adds a six-hour forecast. The large Weather widget also shows the same data, including the six-hour forecast, and adds the forecast for the next five days.
Avoid creating a widget that does nothing but launch your app. People appreciate widgets because they provide instant access to meaningful content. A widget that behaves like an app icon offers no additional value and isn't likely to remain on the user's screen.
Offer your widget in multiple sizes when doing so adds value. In general, avoid simply expanding a smaller widget's content to fill a larger area. It's more important to create one widget in the size that works best for the content you want to display than it is to provide the widget in all sizes.
Prefer dynamic information that changes throughout the day. If a widget's content never appears to change, people may not keep it in a prominent position. Although widgets don't update from minute to minute, it's important to find ways to keep their content fresh to invite frequent viewing.
Look for opportunities to surprise and delight. For example, you might design a unique visual treatment for your calendar widget to display on meaningful occasions, like birthdays or holidays.
Supporting Widget Configuration and Interactivity
Offer a configurable widget when it makes sense. In many cases, people need to specify the information they want to see before a widget can display useful content. For example, people need to choose a location for a Weather widget or a stock symbol for a Stocks symbol widget. In contrast, the Podcasts widget is preconfigured to display recent content, so people don't need to customize it. If you're creating a configurable widget, avoid requiring too many settings or asking for information that might be hard for people to find. You don't have to design a configuration UI for your widget, because the system automatically generates it for you. For developer guidance, see Making a Configurable Widget.
Ensure that tapping your widget opens your app at the right location. When people tap your widget, it deep-links into your app, where you can offer details and actions that are directly related to the widget's content. For example, when people tap a Stocks symbol widget, the Stocks app opens to a page that displays information about that symbol. Similarly, when people tap a small Stocks watchlist widget, the app opens to show the complete watchlist.
Avoid defining too many tap targets. A small widget supports a single tap target, but medium and large widgets can offer multiple targets. https://downloadtherapy.mystrikingly.com/blog/how-to-buy-minecraft-pocket-edition. For example, the medium Notes widget can display several notes. When people tap one of them, the app opens to display that note.
Although multiple tap targets might make sense for your content, avoid offering so many targets that people have trouble tapping the one they want.
Let people know when authentication adds value. If your widget provides additional functionality when someone is logged in to your app, make sure people know that. For example, an app that shows upcoming reservations might include a message like 'Sign in to view reservations' when people aren't logged in.
Updating Widget Content
Keep your widget up-to-date. To remain relevant and useful, widgets should periodically refresh their information. Waltr 2 6 25 inch. Widgets don't support continuous, real-time updates, and the system may adjust the limits for updates depending on various factors. Finding the right update frequency for your widget depends on knowing how often the data changes and estimating how often people need to see new data. For example, a widget that helps people track a beach's tides could provide useful information on an hourly basis, even though tide conditions change constantly. If people are likely to check your widget more frequently than you can update it, consider displaying text that describes when the data was last updated. For developer guidance, see Keeping a Widget Up to Date.
Let the system update dates and times in your widget. Widget update frequency is limited, and you can preserve some of your update opportunities by letting the system refresh date and time information.
Show content quickly. When you determine the update frequency that fits with the data you display, you shouldn't need to hide stale data behind placeholder content.
Designing a Beautiful Widget
In iOS 14 and later, widgets use rich, bold colors, evocative images, and clear, crisp text that's easy to read at a glance. A unique, beautiful widget not only provides useful information, it also gives people an opportunity to personalize the Home screen.
Help people recognize your widget by including design elements linked to your brand's identity. Design elements like brand colors, typeface, and stylized icons are great for making a widget instantly recognizable, but they can be easy to overuse. Take care to keep brand-related design elements from crowding out useful information or making your widget look out of place on the Home screen or Today View.
Avoid displaying a logo, wordmark, or app icon in your widget. The exception to this is when your widget displays curated content from multiple sources. In this case, a small logo in the upper-right corner of the widget subtly reminds people that your app is delivering the content. It can also be helpful to display a small logo or wordmark to attribute the source of the content your widget displays.
Aim for a comfortable density of information. When content appears sparse, the widget can seem unnecessary; when content is too dense, the widget isn't glanceable. If you have lots of information to include, avoid letting your widget become a collage of items that are difficult to parse. Seek ways to curate the content so that people can grasp the essential parts instantly, and view relevant details at a longer look. You might also consider creating a larger widget and looking for places where you can replace text with graphics without losing clarity.
Use color judiciously. Rich, beautiful colors draw the eye, but they should never prevent people from absorbing a widget's information at a glance. Use colors to enhance a widget's appearance without competing with its content. In your asset catalog, you can also specify the colors you want the system to use as it generates your widget's configuration UI.
Support Dark Mode. A widget should look great in both the light and dark appearances. In general, avoid displaying dark text on a light background for the dark appearance, or light text on a dark background for the light appearance. When you use the semantic system colors for text and backgrounds, the colors dynamically adapt to the current appearance. You can also support different appearances by putting color variants in your asset catalog. For guidance, see Dark Mode (iOS) and Dark Mode (macOS); for developer guidance, see About Asset Catalogs.
Consider using SF Pro. Using the system font helps your widget look at home on any platform, and makes it easy to display great-looking text in a variety of weights, styles, and sizes. If you need to use a custom font, consider using it sparingly and be sure it's easy to read at a glance. It often works well to use a custom font for the large text in a widget and use SF Pro for the smaller text. For guidance, see Typography (iOS) and Typography (macOS).
Always use text elements in a widget to ensure that your text scales well. In particular, don't rasterize text — doing so prevents VoiceOver from speaking your content.
Consider using SF Symbols. SF Symbols helps you align and scale symbols with text that uses SF Pro. For guidance, see SF Symbols.
Design a realistic preview to display in the widget gallery. Highlighting your widget's appearance and capabilities helps people make an informed decision and can encourage them to add your widget. You can display real data in your widget preview, but if the data takes too long to generate or load, display realistic simulated data instead.
Design placeholder content that helps people recognize your widget. A widget displays placeholder content while its data loads. You can create an effective preview by combining static parts of the UI with semi-opaque shapes that stand in for actual content. For example, you can use rectangles of different widths to suggest lines of text, and circles or squares in place of glyphs and images.
Avoid mirroring your widget's appearance within your app. People know that tapping a widget or other Home screen element opens a view within an app. If your app displays an element that looks like your widget but doesn't behave like it, people can be confused when the element responds differently to their tap. Also, people may be less likely to try other ways to interact with such an element in your app, because they expect it to behave like a widget.
Write a succinct description of your widget. The widget gallery displays descriptions that can help people understand what each widget does. It generally works well to begin a description with an action verb; for example, 'See the current weather conditions and forecast for a location' or 'Keep track of your upcoming events and meetings.' Avoid including unnecessary phrases that reference the widget itself, like 'This widget shows.,' 'Use this widget to.,' or 'Add this widget.' Use approachable language and sentence-style capitalization.
Adapting to Different Screen Sizes
Widgets scale to adapt to the screen sizes of different devices and onscreen areas. Ensure that your widget looks great on every device by supplying content at appropriate sizes.
Size images to look great on large devices and at high scale factors. On small devices and at low scale factors, images may resize to fit smaller dimensions. For example, to make sure that a small widget's background image looks great on large devices, create an image that measures 169x169 pt (507x507 px @3x). When this widget runs on a device with a screen size of 320x568 pt, SwiftUI resizes the image to look good at 141x141 pt. As you create images for various devices and scale factors, use the sizes listed below for guidance.
Screen size (portrait) | Small widget | Medium widget | Large widget |
---|---|---|---|
414x896 pt | 169x169 pt | 360x169 pt | 360x376 pt |
375x812 pt | 155x155 pt | 329x155 pt | 329x345 pt |
414x736 pt | 159x159 pt | 348x159 pt | 348x357 pt |
375x667 pt | 148x148 pt | 322x148 pt | 322x324 pt |
320x568 pt | 141x141 pt | 291x141 pt | 291x299 pt |
Coordinate the corner radius of your content with the corner radius of the widget. To ensure that your content looks good within a widget's rounded corners, use a SwiftUI container to apply the correct corner radius. For developer guidance, see ContainerRelativeShape.
Civilization: beyond earth 1 1 4. Ensure text and glyphs adapt to size changes. Consider using SF Pro and SF Symbols to help your text and glyphs look great and remain aligned at every size.
NOTE In iOS, widgets support Dynamic Type sizes from Large to xxxLarge when you use Font to choose a system font or custom(_:size:) to choose a custom font.
In general, use standard margins to ensure your content is comfortably legible. The standard margin width is 16 points. If your widget displays content like text, glyphs, and graphs, use the standard margins to avoid crowding the edges and creating a cluttered appearance. If you use background shapes to create visual content groupings, or if you display button backgrounds, you might need to use tight margins. Tight margins — which measure 8 points in width — can also help make graphics that contain information easier for people to read.
With NotificationCenter you can broadcast data from one part of your app to another. It uses the Observer pattern to inform registered observers when a notification comes in, using a central dispatcher called Notification Center. Playtika rewards promotional code.
In this article we'll find out how the notification center mechanism works, and how you can use it in your apps. As you'll see, Notification Center is particularly useful for getting data from one part of your app to another.
Before we get started, some quick notes:
- The
NotificationCenter
class used to be calledNSNotificationCenter
– with the 'NS' prefix – prior to Swift 3. You'll often find people still referring to it as NSNotificationCenter. - The
NotificationCenter
mechanism has nothing to do with push notifications, local notifications or the Notification Center UI, an overview of app notifications as found in iOS and macOS. - This guide solely focuses on using
NotificationCenter
to send in-app broadcasts with Swift 4.
Let's continue.
How Notification Center Works
We'll start with how NotificationCenter exactly works. It has three components:
- A 'listener' that listens for notifications, called an observer
- A 'sender' that sends notifications when something happens
- The notification center itself, that keeps track of observers and notifications
The mechanism is simple. Let's look at an analogy:
- You're working in a mail processing facility that delivers mail to 5 coloured streets
- You, the postman, need to deliver purple letters to purple street whenever they arrive at the mail processing facility
- When your shift starts, you tell the central command of the mail facility – let's call him Bob – that you want to know when purple mail arrives
- Some time passes, and Alice – who lives on yellow street – decides to send a letter to purple street
- So, she sends her letter to the mail processing facility, where it is picked up by Bob – the central command – who informs you that a purple letter has arrived
- You get the purple letter from Bob, and safely deliver it on purple street
The cool thing about this mechanism is that Bob and Alice don't need to know each other, they only need to 'know' the central command – the NotificationCenter
.
Also, the Notification Center can handle one-to-many and many-to-many communications. Alice can send stuff to Bob, Arthur can send stuff to Bob, and if Alice is listening in, Bob and Arthur can also send stuff to Alice!
Back to the mechanism. Here's an overview of the flow of data for NotificationCenter:
This is what happens:
- Component B tells NotificationCenter that it wants to observe a notification with
addObserver(_:selector:name:object:)
- Something happens in Component A, like data coming in or a task that completes
- Component A creates a notification and posts it to the Notification Center
- The Notification Center notifies Component B that a notification it observes has been posted by calling a selector on Component B
- The selector is called, effectively executing a function of Component B
Component A and B can be any kind of class, such as a view controller, a custom API class or a data model.
Makes sense? Let's move on!
Learn how to build iOS apps
Get started with iOS 14 and Swift 5Sign up for my iOS development course, and learn how to build great iOS 14 apps with Swift 5 and Xcode 12.
How To Register Notification Observers
Here's how you register an observer:
This adds an entry with those parameters to the Notification Center, essentially telling it that self
wants to observe for notifications with name .didReceiveData
, and when that notification occurs, the function onDidReceiveData(_:)
should be called.
The addObserver(_:selector:name:object:)
function has 4 parameters:
- The first parameter
observer
is unnamed, and you provide it with a reference to the object that is the observer for this notification, which often isself
- The second parameter
selector
is the function you want to call when the notification occurs, which works like the target-action pattern - The third parameter
name
is the name of the notification you want to listen for, which is of typeNotification.Name
- The fourth parameter
object
is an optional object whose notifications you want to receive, so if you set it, you'll only receive notifications from that 'sender'
It's smart to think of the observer
and the selector
as the target and the action. What function, a so-called selector, do you want to call on what object, a so-called observer, when the notification occurs?
Note: In Objective-C, a selector is a type that refers to the name of an Objective-C method. In Swift, we just call it a function – although there's a nuance. You define the selector by typing #selector()
, with the function name written within the parentheses.
It's important to use the correct selector here, including the right function parameters, so it's easiest to use Xcode's code auto-completion with Esc
after placing the cursor in between the parentheses. Try it!
The function you want to call when a notification is observed, can be anything. In the example above, it could look like this:
The function has one parameter, which is a Notification
object. You'll see later on that you can use property userInfo
to send some data with the notification.
Note: You need to mark this function with . That attribute makes your Swift function available in the Objective-C runtime. The NotificationCenter
is part of the Objective-C runtime, so in order for it to call your function, you'll need to add that attribute.
Every notification has a name of type Notification.Name
. You can create it like this:
It's more convenient to add your notification names as static constants to an extension for Notification.Name
, like this:
The notification names are now available as static constants on the Notification.Name
type, so you can use this syntax:
I personally often use the 'did do something' or 'has downloaded something' format for naming notifications, and I'm naming notification selectors similarly with 'on did something' or onDidSomething(_:)
.
A key part of using NotificationCenter
is that you register an observer before the notification you want to observe, occurs. When a notification is posted to the notification center, and you're not observing yet, you will obviously miss the notification.
By the way, the default
property of the NotificationCenter
is, in fact, the default notification center (duh!). All system notifications, such as UIKeyboardDidHide
and UIKeyboardDidShow
, are posted there.
You can also create custom notification centers, although you probably don't need to. When you need to filter notifications, use the notification name and _sender_object
.
It's common to register and remove observers in either:
viewDidLoad()
anddealloc
, orviewWillAppear(_:)
andviewWillDisappear(_:)
You can remove an observer with removeObserver(_:name:object:)
and using the exact same parameters as for addObserver(.)
. You can also remove all observers with removeObserver(_:)
.
One last thing… Observers need to be removed before they are deallocated, or you risk sending a message to an object that doesn't exist.
- The first parameter
observer
is unnamed, and you provide it with a reference to the object that is the observer for this notification, which often isself
- The second parameter
selector
is the function you want to call when the notification occurs, which works like the target-action pattern - The third parameter
name
is the name of the notification you want to listen for, which is of typeNotification.Name
- The fourth parameter
object
is an optional object whose notifications you want to receive, so if you set it, you'll only receive notifications from that 'sender'
It's smart to think of the observer
and the selector
as the target and the action. What function, a so-called selector, do you want to call on what object, a so-called observer, when the notification occurs?
Note: In Objective-C, a selector is a type that refers to the name of an Objective-C method. In Swift, we just call it a function – although there's a nuance. You define the selector by typing #selector()
, with the function name written within the parentheses.
It's important to use the correct selector here, including the right function parameters, so it's easiest to use Xcode's code auto-completion with Esc
after placing the cursor in between the parentheses. Try it!
The function you want to call when a notification is observed, can be anything. In the example above, it could look like this:
The function has one parameter, which is a Notification
object. You'll see later on that you can use property userInfo
to send some data with the notification.
Note: You need to mark this function with . That attribute makes your Swift function available in the Objective-C runtime. The NotificationCenter
is part of the Objective-C runtime, so in order for it to call your function, you'll need to add that attribute.
Every notification has a name of type Notification.Name
. You can create it like this:
It's more convenient to add your notification names as static constants to an extension for Notification.Name
, like this:
The notification names are now available as static constants on the Notification.Name
type, so you can use this syntax:
I personally often use the 'did do something' or 'has downloaded something' format for naming notifications, and I'm naming notification selectors similarly with 'on did something' or onDidSomething(_:)
.
A key part of using NotificationCenter
is that you register an observer before the notification you want to observe, occurs. When a notification is posted to the notification center, and you're not observing yet, you will obviously miss the notification.
By the way, the default
property of the NotificationCenter
is, in fact, the default notification center (duh!). All system notifications, such as UIKeyboardDidHide
and UIKeyboardDidShow
, are posted there.
You can also create custom notification centers, although you probably don't need to. When you need to filter notifications, use the notification name and _sender_object
.
It's common to register and remove observers in either:
viewDidLoad()
anddealloc
, orviewWillAppear(_:)
andviewWillDisappear(_:)
You can remove an observer with removeObserver(_:name:object:)
and using the exact same parameters as for addObserver(.)
. You can also remove all observers with removeObserver(_:)
.
One last thing… Observers need to be removed before they are deallocated, or you risk sending a message to an object that doesn't exist.
How To Post Notifications
Now that we're observing for a notification, let's post it! Posting a notification is simple:
The function post(name:object:userInfo:)
has 3 parameters:
- The first parameter is
name
, the notification name, of typeNotification.Name
. It's exactly the same as the notification you're observing, and it's smart to use that extension here too. - The second parameter is
object
, the sender of the notification. You can leave it empty withnil
or use any object. It's purpose is the same as theobject
parameter ofaddObserver(.)
, so you can use it to filter the sender of a notification. - The third parameter is
userInfo
, and you can use it to send extra data with the notification. The parameter is not required, so you can leave it out if you don't need it.
Let's say you want to display some data in a table view. The data comes from a JSON webservice, and contains the game scores of Bob, Alice and Arthur.
You register for the .didReceiveData
notification in the table view controller, like this:
And the onDidReceiveData(_:)
function looks like this:
In the example above, this happens:
- First, you're using conditional binding to downcast the type of
userInfo
to[String: Int]
with the type cast operatoras?
. Because the type ofuserInfo
is[AnyHashable: Any]?
– basically any dictionary – you can downcast its type to the dictionary type you're expecting. That's string keys, integer values in the example above. If the cast succeeds, thendata
contains the value ofuserInfo
as type[String: Int]
. - Then, using a
for-in
loop, the items in thedata
dictionary are iterated. We're using a tuple(name, score)
to decompose the key and value of the dictionary items. - Finally, inside the loop, we're printing out the names and scores of Bob, Alice and Arthur.
When we want to post the notification that contains the data, we do this:
See how the scores
dictionary is provided as the argument for userInfo
? We're basically sending those score data alongside the notification. They'll get picked up in onDidReceiveData(_:)
and printed out.
Another thing stands out in the example above: the usage of object
. When registering the observer, you're specifying the sender you want to receive notifications from. Like this:
Then, when posting the notification we specify the object
parameter too. Like this:
You already know that self
refers to the current instance of the class we're in. So when API.shared
references the same object as self
, the notification is picked up by the observer.
Here's what that class could look like:
And that's how we post notifications! Let's continue with some use cases…
NotificationCenter In Practical iOS Development
Let's start by pointing out when you shouldn't use NotificationCenter
.
The notification center mechanism is so simple and convenient, that it's tempting to let any and all objects, view controllers and models communicate with it.
Say you have a View Controller A and a View Controller B. You want to pass some data from A to B, when B is shown on screen. Do you use the notification center? No!
It's much simpler and clearer to use a function or property on View Controller B, like setScores(_:)
or currentUser
, than it is to use notification center. You might even want to use a closure, or a set a property in a segue, or just use any of the standard principles of MVC.
Keep in mind to loosely couple your view controllers and/or objects, for instance by using a protocol.
Want to know how to pass data between view controllers, in any scenario? Check out the extensive blog post I wrote on that topic.
So when do you use NotificationCenter
? I use the following guidelines:
- When communication between two or more components of your app, that have no formal connection, needs to happen
- When communication needs to happen repeatedly and consistently
- When using one-to-many or many-to-many communication
Imagine you have a table view controller and an API. The data from the API needs to end up in the table view controller, after it has been downloaded from a web-based JSON resource. Both classes have single responsibilities, respectively getting the data and showing the data.
Swift Note 1 0 – Widget For Notifications Centers
It doesn't make sense to connect the table view controller to the API directly. You're not going to add a tableView
property to the API
class, and add data to its data source when the JSON has been downloaded.
Likewise, it doesn't make sense to add a dataCollection
property to the API
class and let the table view controller use that directly. You likely want to use a datastore anyway.
Swift Note 1 0 – Widget For Notifications Centered
No, you want to loosely couple the table view controller and the API, for two reasons:
- So you can use the data from the API for something else, like search, or another view controller with a different layout
- So you can use the table view controller for something else, like displaying search results or showing other different but similarly looking data
Because of that, you end up using NotificationCenter
. This has a few added benefits:
- When another part of your app also needs to respond to new data coming in, it can simply listen in for the same notification name. This is one-to-many communication, because the one API can post notifications to many observers.
- When that same table view controller is used to display different data, for instance for a search feature, it can observe a different notification name or distinguish notifications by their sender. This is many-to-one or many-to-many communication, because now two view controllers are listening in on data from different sources.
Finally, I think that the determining guideline for using Notification Center should be repeat and consistent use. That connection between the table view controller and the API is like an information superhighway, moving data from one part of your app to the next. Every time the user 'pulls to refresh', initiates a search, or needs new data, a signal is sent to the API to download new data, after which a notification is posted.
Swift Note 1 0 – Widget For Notifications Centerpieces
Learn how to build iOS apps
Get started with iOS 14 and Swift 5Sign up for my iOS development course, and learn how to build great iOS 14 apps with Swift 5 and Xcode 12.
Further Reading
So! Notification Center. Now you know… Here's a quick summary:
- First, register an observer for a notification with:
addObserver(_:selector:name:object:)
- Then, post a notification with
post(name:object:userInfo:)
… - … after which your selector is called
- And don't forget to remove the observer with
removeObserver(.)
You use NotificationCenter
for repeat, consistent one-to-many or many-to-many communications. If you're not sure, check out how to pass data between view controllers. And you can find a simple example in Xcode here.
Want to learn more? Check out these resources: