我有一个 iPad 应用程序,它使用一个注册“UIScreenDidConnectNotification”的专有库对象。有时,这个对象在幕后被释放和重新分配。因为它在图书馆中,所以我无法确保它正确地删除了这个观察者。
有没有办法让我手动删除特定通知的所有/任何观察者(即 UIScreenDidConnectNotification),而无需访问已注册的对象。这将阻止应用程序将消息发送到已释放的对象。
更新:这是解决我的问题的最简单方法。我希望我能做得更好,但生命太短暂了。#导入 #导入
@interface NSNotificationCenter (AllObservers)
@end
@implementation NSNotificationCenter (AllObservers)
// This function runs before main to 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);
// This function runs before main to 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 this function to remove any unwieldy behavior for adding observers
*/
- (void) swizzled_addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender
{
NSString *notification = [[NSString alloc] initWithUTF8String: "UIScreenDidConnectNotification" ];
// It's a hack, but I just won't allow my app to add this type of notificiation
if([notificationName isEqualToString: notification])
{
printf("### screen notifcation added for an observer: %s\n", [notificationSender UTF8String] );
}
else
{
// Calls the original addObserver function
[self swizzled_addObserver:notificationObserver selector:notificationSelector name:notificationName object:notificationSender];
}
}