ipad - Objective-C remove all oberservers for a certain notification -
i have ipad app uses proprietary library object registers "uiscreendidconnectnotification". object deallocated , reallocated behind scenes. in library, cannot ensure removing observer.
is there way me manually remove all/any observers specific notification (i.e. uiscreendidconnectnotification) without having access object has registered. keep application sending message deallocated object.
update: here easiest way fix problem. wish better job, life short. #import #import
@interface nsnotificationcenter (allobservers) @end @implementation nsnotificationcenter (allobservers) // function runs before main swap in our special version of addobserver + (void) load { method original, swizzled; original = class_getinstancemethod(self, @selector(addobserver:selector:name:object:)); swizzled = class_getinstancemethod(self, @selector(swizzled_addobserver:selector:name:object:)); method_exchangeimplementations(original, swizzled); // function runs before main swap in our special version of addobserver + (void) load { method original, swizzled; original = class_getinstancemethod(self, @selector(addobserver:selector:name:object:)); swizzled = class_getinstancemethod(self, @selector(swizzled_addobserver:selector:name:object:)); method_exchangeimplementations(original, swizzled); } /* use function remove unwieldy behavior adding observers */ - (void) swizzled_addobserver:(id)notificationobserver selector:(sel)notificationselector name:(nsstring *)notificationname object:(id)notificationsender { nsstring *notification = [[nsstring alloc] initwithutf8string: "uiscreendidconnectnotification" ]; // it's hack, won't allow app add type of notificiation if([notificationname isequaltostring: notification]) { printf("### screen notifcation added observer: %s\n", [notificationsender utf8string] ); } else { // calls original addobserver function [self swizzled_addobserver:notificationobserver selector:notificationselector name:notificationname object:notificationsender]; } }
i agree both of caleb's points: not responsibility perform task , there nothing in api support it.
however... if feel hacking in perform task whatever reason, refer thread: how retrieve nsnotificationcenter observers?
the selected answer of thread has category nsnotificationcenter
allows retrieve observers given notification name. again, not recommended though.
Comments
Post a Comment