How to properly use CFNotificationCenterAddObserver in Swift for iOS

前端 未结 2 907
面向向阳花
面向向阳花 2020-12-28 20:42

Pulling my hair out getting CFNotificationCenterAddObserver to work in Swift.

        CFNotificationCenterAddObserver(CFNotificationCenterGetDa         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-28 21:33

    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 
    
    #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;
    
    @end
    
    #endif
    

    DarwinNotificationsManager.m :

    #import 
    #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];
        callback();
    }
    
    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));
    }
    
    
    @end
    

    In swift you can use it like this :

    let darwinNotificationCenter = DarwinNotificationsManager.sharedInstance()
    darwinNotificationCenter.registerForNotificationName("YourNotificationName"){
                //code to execute on notification
    }
    

提交回复
热议问题