Skip to content

A mechanism for sending typed notifications with payloads across your iOS app.

License

Notifications You must be signed in to change notification settings

mergesort/TypedNotifications

Repository files navigation

TypedNotifications

A wrapper around NotificationCenter for sending typed notifications with payloads across your iOS app.

BuddyBuild Pod Version Swift Version License MIT Plaform

Asynchronous behavior is hard. Let's make it a little easier so we can turn that frown (πŸ™) upside down (πŸ™ƒ).


Using TypedNotifications is easy. You can drop it into your app and replace all of your un-typed Notifications in minutes.


Registering for Notifications

You can register notifications for either payload containing notifications, or payload-free notifications.

func register<T: TypedNotification>(type: T.Type, observer: Any, object: Any? = nil, selector: Selector)
func register<T: TypedPayloadNotification>(type: T.Type, observer: Any, object: Any? = nil, selector: Selector)

Sending Notifications

You can send notifications for either payload containing notifications, or payload-free notifications.

func post<T: TypedNotification>(typedNotification: T, object: Any? = nil)
func post<T: TypedPayloadNotification>(typedNotification: T, object: Any? = nil)

Extracting values from Notifications

Only payload containing notifications can have their payload extracted, because, duh.

func getPayload<T: TypedPayloadNotification>(notificationType: T.Type) -> T.Payload?

Now that might look a little scary at first with all those Ts, but let's break it down with some examples and show you how easy this is.

Examples

Create some values you'd like to send through your app.

enum Job {
    
    case softwareDeveloper
    case designer
    case conArtist

}

struct Person {

    let name: String
    let job: Job

}

Create the notification to send your value

If you have no payload and just want to send a message, use a TypedNotification like so.

struct SomeEventNotification: TypedNotification {}

For our example, let's use a TypedPayloadNotification with a payload though.

struct TypedPersonNotification: TypedPayloadNotification {

    let payload: Person

}

Register the notification

NotificationCenter.default.register(type: TypedPersonNotification.self, observer: self, selector: #selector(personNotificationWasReceived))

Send the notification

let amanda = Person(name: "Amanda", job: .softwareDeveloper)
let amandaNotification = TypedPersonNotification(payload: amanda)
NotificationCenter.default.post(typedNotification: amandaNotification)

And handle the notification

@objc func personNotificationWasReceived(notification: Notification) {
    guard let person = notification.getPayload(notificationType: TypedPersonNotification.self) else {
        os_log("Could not properly retrieve payload from TypedPersonNotification")
        return
    }
    
    let nameText = "Name: \(person.name)"
    let jobText = "Job: \(person.job.title)"

    print("Got our Person payload!\n\(nameText)\n\(jobText)")
}

And that's it! You've sent a typed notification throughout your app.

If you want to play on expert mode, I recommend using generics and passing notifications through your app that way.

struct GenericTypedPayloadNotification<T>: TypedPayloadNotification {

    let payload: T

}

Requirements

  • iOS 8.0+
  • Xcode 7.3+

Installation

SPM will be the default supported installation method from version 1.4.0 and higher, so please integrate by using SPM.

If you're still using CocoaPods for version 1.4.0 or below you can install TypedNotifications by adding it to your Podfile:

platform :ios, '8.0'
use_frameworks!

pod 'TypedNotifications'

Or install it manually by downloading TypedNotifications.swift and dropping it in your project.

About me

Hi, I'm Joe everywhere on the web, but especially on Twitter.

License

See the license for more information about how you can use TypedNotifications.