2010-10-19 85 views
2

我正在寫一個小測試程序,我可以查看iOS設備上發生的各種操作時發佈的所有通知。不幸的是,當我嘗試向達爾文通知中心添加觀察者時,我遇到了出現EXC_BAD_ACCESS錯誤的問題。相關代碼如下:傾聽所有CFNotifications?

void callback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) { 
    NSNotification* n = [NSNotification notificationWithName:name object:object userInfo:userInfo]; 

    objc_msgSend(gSelf, sel_getUid(@"note:"), n); 
    return; 
} 


- (void)viewDidLoad { 
    [super viewDidLoad]; 
    gSelf = self; 
    notifications = [[NSMutableArray alloc] initWithCapacity:10]; 
    self.title = @"Notification Log"; 

    CFNotificationCenterAddObserver( 
            CFNotificationCenterGetDarwinNotifyCenter(), //center 
            NULL, // observer 
            callback, // callback 
            NULL, // name 
            NULL, // object 
            CFNotificationSuspensionBehaviorHold 
            ); 
} 

-(void)note:(NSNotification *)notification{ 

    @synchronized(self){ 
     [notifications addObject:notification]; 
     [self.tableView reloadData]; 
    } 
} 

回答

4

從CFNotificationCenter.h:(查找的CFNotificationCenterGetDarwinNotifyCenter定義。)或the CFNotificationCenter reference

CFNotificationCenterAddObserver(): '名字' 的說法可能不是NULL(用於這個中心)。

+1

謝謝。我應該爲任何希望聽取大多數CFNotifications的人添加:http://blogs.oreilly.com/iphone/2008/08/iphone-notifications.html提供了一種方法來獲取需要傳遞到達爾文中心的字符串。 – 2010-10-19 18:48:02

+0

David的評論鏈接到Erica Sadun的博客文章,主要解釋了她如何創建'notificationWatcher'越獄工具。 – Nate 2013-02-15 08:39:17