How to properly use CFNotificationCenterAddObserver in Swift for iOS

Pulling my hair out getting CFNotificationCenterAddObserver to work in Swift.

        "" as CFString,

The iOS docs have it listed and I have tried countless iterations on the callback and the unsafe pointer with no success.

  • Add a “Full Name” Field to Firebase Email/Password Authentication?
  • Swift - using timers/system clock/background threads to do something in my application every hour
  • Can you addTarget to UIButton in NSObject class?
  • Drop down list ios swift
  • UIPickerView selectRow doesn't works
  • How to open Acrobat Reader to read pdf file?
  • The above function call results in this error message, which seems to be the correct init:

    Cannot invoke 'init' with an argument list of type '(CFNotificationCenter!, $T4, () -> (), CFString, NilLiteralConvertible, CFNotificationSuspensionBehavior)'

    I also tried bridging to objc as this post here suggests, but without success.

    Here is my bridge:


    #import <Foundation/Foundation.h>
    @interface LockNotifierCallback : NSObject
    + (void(*)(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo))notifierProc;

    and LockNotifierCallback.m:

    #import "LockNotifierCallback.h"
    static void lockcompleteChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
    @implementation LockNotifierCallback
    + (void(*)(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo))notifierProc {
        return lockcompleteChanged;

    with updated CFNotificationCenterAddObserver call as follows:

            "" as CFString,

    and of course LockNotifierCallback.h is in my Bridging header. Error continues:

    Cannot convert the expression's type '(CFNotificationCenter!, () -> CFunctionPointer<((CFNotificationCenter!, UnsafeMutablePointer<Void>, CFString!, UnsafePointer<Void>, CFDictionary!) -> Void)>, () -> (), CFString, NilLiteralConvertible, CFNotificationSuspensionBehavior)' to type 'StringLiteralConvertible'

    Solutions Collect From Internet About “How to properly use CFNotificationCenterAddObserver in Swift for iOS”

    I had some issues with DarwinNotifications, you can try using this wrapper class
    just include header file in your bridging file. And you can use it in swift.

    DarwinNotificationsManager.h :

    #import <Foundation/Foundation.h>
    #ifndef DarwinNotifications_h
    #define DarwinNotifications_h
    @interface DarwinNotificationsManager : NSObject
    @property (strong, nonatomic) id someProperty;
    + (instancetype)sharedInstance;
    - (void)registerForNotificationName:(NSString *)name callback:(void (^)(void))callback;
    - (void)postNotificationWithName:(NSString *)name;

    DarwinNotificationsManager.m :

    #import <Foundation/Foundation.h>
    #import "DarwinNotificationsManager.h"
    @implementation DarwinNotificationsManager {
        NSMutableDictionary * handlers;
    + (instancetype)sharedInstance {
        static id instance = NULL;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            instance = [[self alloc] init];
        return instance;
    - (instancetype)init {
        self = [super init];
        if (self) {
            handlers = [NSMutableDictionary dictionary];
        return self;
    - (void)registerForNotificationName:(NSString *)name callback:(void (^)(void))callback {
        handlers[name] = callback;
        CFNotificationCenterRef center = CFNotificationCenterGetDarwinNotifyCenter();
        CFNotificationCenterAddObserver(center, (__bridge const void *)(self), defaultNotificationCallback, (__bridge CFStringRef)name, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
    - (void)postNotificationWithName:(NSString *)name {
        CFNotificationCenterRef center = CFNotificationCenterGetDarwinNotifyCenter();
        CFNotificationCenterPostNotification(center, (__bridge CFStringRef)name, NULL, NULL, YES);
    - (void)notificationCallbackReceivedWithName:(NSString *)name {
        void (^callback)(void) = handlers[name];
    void defaultNotificationCallback (CFNotificationCenterRef center,
                     void *observer,
                     CFStringRef name,
                     const void *object,
                     CFDictionaryRef userInfo)
        NSLog(@"name: %@", name);
        NSLog(@"userinfo: %@", userInfo);
        NSString *identifier = (__bridge NSString *)name;
        [[DarwinNotificationsManager sharedInstance] notificationCallbackReceivedWithName:identifier];
    - (void)dealloc {
        CFNotificationCenterRef center = CFNotificationCenterGetDarwinNotifyCenter();
        CFNotificationCenterRemoveEveryObserver(center, (__bridge const void *)(self));

    In swift you can use it like this :

    let darwinNotificationCenter = DarwinNotificationsManager.sharedInstance()
                //code to execute on notification