Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A tiny Swift package that makes it easy to embed strongly-typed values in notifications.

License

NotificationsYou must be signed in to change notification settings

samsonjs/NotificationSmuggler

Repository files navigation

0 dependencies!

Overview

NotificationSmuggler is a tiny Swift package that makes it easy to embed strongly-typed values inNotifications, and extract them out on the receiving end as well. Nothing elaborate, it sneaks the contraband in theuserInfo dictionary.

Declare a type conforming toSmuggled and then use the static methodNotification.smuggle(_:object:) when posting the notification. On the receiving side of things you can use the extension methodsNotificationCenter.notifications(for:object:) andNotificationCenter.publisher(for:object:) to observe the strongly-typed values without manually mapping them yourself.

If you haveSendable contraband then all of this will work nicely with Swift 6 and complete concurrency checking.

This package pairs nicely withAsyncMonitor for a complete notification handling system in the Swift 6 concurrency world.

Usage

Define a smuggled payload

structSomeNotification:Smuggled,Sendable{letanswer:Int}

Your payload doesn't have to be Sendable but if it is then you have more flexibility.

TheSmuggled protocol provides staticnotificationName anduserInfoKey properties for you, should you need them. Generally you don't though.

Post a notification

NotificationCenter.default.smuggle(SomeNotification(answer:42))

or

NotificationCenter.default.post(.smuggle(SomeNotification(answer:42)))

Both automatically set the.name,userInfo, and optionally.object for the notification.

Observe and extract contraband

Task{    // This is fine because SomeNotification is SendableforawaitnotificationinNotificationCenter.default.notifications(for:SomeNotification.self){print(notification.answer)}}

Installation

The only way to install this package is with Swift Package Manager (SPM). Pleasefile a new issue or submit a pull-request if you want to use something else.

Supported Platforms

This package is supported on iOS 18.0+ and macOS 15.0+.

Xcode

When you're integrating this into an app with Xcode then go to your project's Package Dependencies and enter the URLhttps://github.com/samsonjs/NotificationSmuggler and then go through the usual flow for adding packages.

Swift Package Manager (SPM)

When you're integrating this using SPM on its own then add this to the list of dependencies your Package.swift file:

.package(url:"https://github.com/samsonjs/NotificationSmuggler.git",.upToNextMajor(from:"0.2.1"))

and then add"NotificationSmuggler" to the list of dependencies in your target as well.

License

Copyright © 2025Sami Samhurisami@samhuri.net. Released under the terms of theMIT License.

About

A tiny Swift package that makes it easy to embed strongly-typed values in notifications.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp