如何在Swift for iOS中正确使用CFNotificationCenterAddObserver

把我的头发拿出来让CFNotificationCenterAddObserver在Swift中工作。

  CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), UnsafePointer<Void>(self), iosLocked, "com.apple.springboard.lockcomputer" as CFString, nil, CFNotificationSuspensionBehavior.DeliverImmediately) 

iOS文档有它列出,我已经尝试了无数次的callback和不安全的指针,没有成功。

上面的函数调用导致这个错误消息,这似乎是正确的init:

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

我也尝试过桥接objc 这个post在这里build议,但没有成功。

这是我的桥:

LockNotifierCallback.h:

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

和LockNotifierCallback.m:

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

更新CFNotificationCenterAddObserver调用如下:

 CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), LockNotifierCallback.notifierProc, iosLocked, "com.apple.springboard.lockcomputer" as CFString, nil, CFNotificationSuspensionBehavior.DeliverImmediately) 

当然LockNotifierCallback.h是在我的桥接头。 错误继续:

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

我遇到了DarwinNotifications的一些问题,你可以尝试使用这个包装类,只是在你的桥接文件中包含头文件。 而且你可以快速使用它。

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

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]; 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 

在swift中,你可以像这样使用它:

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