How to properly use CFNotificationCenterAddObserver in Swift for iOS -
pulling hair out getting cfnotificationcenteraddobserver
work in swift.
cfnotificationcenteraddobserver(cfnotificationcentergetdarwinnotifycenter(), unsafepointer<void>(self), ioslocked, "com.apple.springboard.lockcomputer" cfstring, nil, cfnotificationsuspensionbehavior.deliverimmediately)
the ios docs have listed , have tried countless iterations on callback , unsafe pointer no success.
the above function phone call results in error message, seems right init:
cannot invoke 'init' argument list of type '(cfnotificationcenter!, $t4, () -> (), cfstring, nilliteralconvertible, cfnotificationsuspensionbehavior)'
i tried bridging objc this post here suggests, without success.
here bridge:
locknotifiercallback.h:
#import <foundation/foundation.h> @interface locknotifiercallback : nsobject + (void(*)(cfnotificationcenterref center, void *observer, cfstringref name, const void *object, cfdictionaryref userinfo))notifierproc; @end
and 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 { homecoming lockcompletechanged; } @end
with updated cfnotificationcenteraddobserver phone call follows:
cfnotificationcenteraddobserver(cfnotificationcentergetdarwinnotifycenter(), locknotifiercallback.notifierproc, ioslocked, "com.apple.springboard.lockcomputer" cfstring, nil, cfnotificationsuspensionbehavior.deliverimmediately)
and of course of study locknotifiercallback.h in bridging header. error continues:
cannot convert expression's type '(cfnotificationcenter!, () -> cfunctionpointer<((cfnotificationcenter!, unsafemutablepointer<void>, cfstring!, unsafepointer<void>, cfdictionary!) -> void)>, () -> (), cfstring, nilliteralconvertible, cfnotificationsuspensionbehavior)' type 'stringliteralconvertible'
i had issues darwinnotifications, can seek using wrapper class include header file in bridging file. , can utilize 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; @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]; }); homecoming instance; } - (instancetype)init { self = [super init]; if (self) { handlers = [nsmutabledictionary dictionary]; } homecoming 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 can utilize :
let darwinnotificationcenter = darwinnotificationsmanager.sharedinstance() darwinnotificationcenter.registerfornotificationname("yournotificationname"){ //code execute on notification }
ios swift notifications darwin
No comments:
Post a Comment