📡 Helping you own NotificationCenter in Swift!

Overview

Notificationz 📡

Helping you own NotificationCenter

Version Version Swift Platforms Carthage

Highlights

  • Keep Your Naming Conventions:
    This library gives you convenient access to NotificationCenter, but it's up to you to set it up the way you like!

  • Nothing to Hide:
    Not trying to hide NotificationCenter functionality. Just an attempt to provide a more convenient API

  • Full and Simple Testing:
    Testing this library was simple, since it only forwards calls to NotificationCenter for the most part. Mocking that object allowed tests to reach 100% coverage.

Features

You can try them in the playground shipped with the framework!

Use your own naming convention to wrap NotificationCenter

let nsCenter = NotificationCenter.default
let 📡 = NotificationCenterAdapter(notificationCenter: nsCenter)
📡.post(.💃)

// my personal preference, define this in Globals.swift
let NC = NotificationCenterAdapter(notificationCenter: nsCenter)
// Now, you can use `NC` throughout the app

Four simple keywords to remember

let obj = Object()
NC.add(obj, selector: #selector(Object.call))   // normal add observer
NC.observe { notification in }                  // observe using blocks
NC.post(.tenhut)                                // post a notification
NC.remove(obj)                                  // remove from nsCenter

Transparent and convenient API

let keys = ["observe", "many", "keys"].map { Notification.Name($0) }
NC.observe(keys) { _ in }       // observe on the same thread
NC.observeUI(keys) { _ in }     // delivered to the main thread

NC.post(.test)
NC.post(.test, userInfo: ["info":5])
NC.post(Notification(name: .test, object: nil))

RAII-based observers

class Dummy {

    // declare the observer as optional
    var broadcastObserver: Observer?

    init() {
        // assign it anywhere you like
        broadcastObserver = NC.observe { [unowned self] _ in
            self.doSomething()
        }.execute() // this is a neat bonus feature
    }

    func doSomething() {
        // exectued twice, since we call "execute" manually
        print("it works!")
    }
}

var dummy: Dummy? = Dummy()
NC.post(.test)  // calls doSomething
dummy = nil     // clean up is automatic
NC.post(.test)  // doesn't crash!

Getting Started

Carthage

Carthage is fully supported. Simply add the following line to your Cartfile:

github "SwiftKitz/Notificationz"

Cocoapods

Cocoapods is fully supported. Simply add the following line to your Podfile:

pod 'Notificationz'

Submodule

For manual installation, you can grab the source directly or through git submodules, then simply:

  • Drop the Notificationz.xcodeproj file as a subproject (make sure Copy resources is not enabled)
  • Navigate to your root project settings. Under "Embedded Binaries", click the "+" button and select the Notificationz.framework

Motivation

After migrating to Swift, the NotificationCenter APIs really stood out in the code. Writing so much boiler plate all over the place just to register, handle, and cleanup notifications. Coming from C++, RAII seemed a pretty invaluable pattern to be applied here.

With this framework, one can easily declare all their observers as properties:

class Sample {
    private var keyboardObserver: Observer?
    private var reachabilityObserver: Observer?
}

Other programmers should be pleased with this consistency! Moreover, there is no need to worry handling notifications in some other function somewhere nor do cleanup in deinit. It just works:

keyboardObserver = NC.observeUI(UIKeyboardWillShowNotification) { [unowned self] _ in
    // you can write your handler code here, maybe call another function
}

// you can force cleanup by setting the property to nil, but don't have to
keyboardObserver = nil

Author

Maz (@Mazyod)

License

Notificationz is released under the MIT license. See LICENSE for details.

You might also like...
Modern thread-safe and type-safe key-value observing for Swift and Objective-C

Now Archived and Forked PMKVObserver will not be maintained in this repository going forward. Please use, create issues on, and make PRs to the fork o

A library for reactive and unidirectional Swift applications
A library for reactive and unidirectional Swift applications

ReactorKit is a framework for a reactive and unidirectional Swift application architecture. This repository introduces the basic concept of ReactorKit

ReSwift is a Redux-like implementation of the unidirectional data flow architecture in Swift.
ReSwift is a Redux-like implementation of the unidirectional data flow architecture in Swift.

ReSwift is a Redux-like implementation of the unidirectional data flow architecture in Swift. ReSwift helps you to separate three important concerns of your app's components.

🐌 snail - An observables framework for Swift
🐌 snail - An observables framework for Swift

🐌 snail A lightweight observables framework, also available in Kotlin Installation Carthage You can install Carthage with Homebrew using the followin

Lightweight Promises for Swift & Obj-C

Tomorrowland Tomorrowland is an implementation of Promises for Swift and Objective-C. A Promise is a wrapper around an asynchronous task that provides

VueFlux is the architecture to manage state with unidirectional data flow for Swift, inspired by Vuex and Flux.
VueFlux is the architecture to manage state with unidirectional data flow for Swift, inspired by Vuex and Flux.

Unidirectional State Management Architecture for Swift - Inspired by Vuex and Flux Introduction VueFlux is the architecture to manage state with unidi

When is a lightweight implementation of Promises in Swift
When is a lightweight implementation of Promises in Swift

Description When is a lightweight implementation of Promises in Swift. It doesn't include any helper functions for iOS and OSX and it's intentional, t

EventBroadcaster is a lightweight event handler framework, written in swift for iOS, macOS, tvOS & watchOS applications.
EventBroadcaster is a lightweight event handler framework, written in swift for iOS, macOS, tvOS & watchOS applications.

EventBroadcaster is a lightweight event handler framework, written in swift for iOS, macOS, tvOS & watchOS applications.

A New, Modern Reactive State Management Library for Swift and SwiftUI (The iOS implementation of Recoil)
A New, Modern Reactive State Management Library for Swift and SwiftUI (The iOS implementation of Recoil)

RecoilSwift RecoilSwift is a lightweight & reactive swift state management library. RecoilSwift is a SwiftUI implementation of recoil.js which powered

Comments
  • Example Project

    Example Project

    Hi I have been trying to get this work though haven't been able to get the notifications to trigger other devices. That what the notifications are meant to do right?

    Thanks in advance for your help.

    opened by Ged2323 2
Releases(v1.0.0)
  • v1.0.0(Dec 18, 2015)

    The library has been in private beta, and I only made it public after shipping it with my apps to the AppStore. You can rest assured it is battle tested.

    Source code(tar.gz)
    Source code(zip)
Owner
Kitz
Collection of quality Swift kits
Kitz
what's the most useless thing you can show on Discord? well, you see,

HealthKitRPC A friend jokingly suggested to have my heart rate visible on Discord. Well... Full of sample code and broken things. Usage Change various

Spotlight 12 Jan 11, 2022
📬 A lightweight implementation of an observable sequence that you can subscribe to.

Features Lightweight Observable is a simple implementation of an observable sequence that you can subscribe to. The framework is designed to be minima

Felix M. 133 Aug 17, 2022
Promises simplify asynchronous programming, freeing you up to focus on the more important things

Promises simplify asynchronous programming, freeing you up to focus on the more important things. They are easy to learn, easy to master and result in

Max Howell 14k Jan 5, 2023
Bond is a Swift binding framework that takes binding concepts to a whole new level.

Bond, Swift Bond Update: Bond 7 has been released! Check out the migration guide to learn more about the update. Bond is a Swift binding framework tha

Declarative Hub 4.2k Jan 5, 2023
Write great asynchronous code in Swift using futures and promises

BrightFutures How do you leverage the power of Swift to write great asynchronous code? BrightFutures is our answer. BrightFutures implements proven fu

Thomas Visser 1.9k Dec 20, 2022
Easy Swift Futures & Promises.

❗️ Archived now ❗️ Since Apple released Combine framework, I decide to archive this repo. You still can use this repo as an example of Future/Promise

Dmytro Mishchenko 40 Sep 23, 2022
Type-safe event handling for Swift

emitter-kit v5.2.2 A replacement for NSNotificationCenter#addObserver and NSObject#addObserver that is type-safe and not verbose. import EmitterKit /

Alec Larson 570 Nov 25, 2022
A Swift based Future/Promises Library for IOS and OS X.

FutureKit for Swift A Swift based Future/Promises Library for IOS and OS X. Note - The latest FutureKit is works 3.0 For Swift 2.x compatibility use v

null 759 Dec 2, 2022
Swift Apps in a Swoosh! A modern framework for creating iOS apps, inspired by Redux.

Katana is a modern Swift framework for writing iOS applications' business logic that are testable and easy to reason about. Katana is strongly inspire

Bending Spoons 2.2k Jan 1, 2023
Observable is the easiest way to observe values in Swift.

Observable is the easiest way to observe values in Swift. How to Create an Observable and MutableObservable Using MutableObservable you can create and

Robert-Hein Hooijmans 368 Nov 9, 2022