Skip to content

Instantly share code, notes, and snippets.

@stuartcarnie
Created January 23, 2022 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuartcarnie/649b777a8f122af9954e021049c84da4 to your computer and use it in GitHub Desktop.
Save stuartcarnie/649b777a8f122af9954e021049c84da4 to your computer and use it in GitHub Desktop.
Watch all notifications posted to Notification Center
// Place this early in your application's startup process, such as an init
let cb: CFNotificationCallback = { (centre: CFNotificationCenter?, observer: UnsafeMutableRawPointer?, name: CFNotificationName?, object: UnsafeRawPointer?, userInfo: CFDictionary?) in
print("Notification: ", terminator: "")
if let name = name {
print("(\(name.rawValue)) ")
} else {
print()
}
if let obj = observer {
let obj2: AnyObject = Unmanaged.fromOpaque(obj).takeUnretainedValue()
if let obj3 = obj2 as? NSObject {
print(" observer: \(obj3.debugDescription)")
}
}
if let obj = object {
let obj2: AnyObject = Unmanaged.fromOpaque(obj).takeUnretainedValue()
if let obj3 = obj2 as? NSObject {
print(" object: \(obj3.debugDescription)")
}
}
if let userInfo = userInfo {
print("\(userInfo)")
}
}
CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(), nil, cb, nil, nil, .deliverImmediately)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment