如何抑制CFUserNotificationDisplayAlert生成的控制台消息

时间:2010-11-07 06:18:31

标签: objective-c cocoa xcode

如果我调用CFUserNotificationDisplayAlert()来显示警告框,它会在控制台中输出以下消息:

CFUserNotificationDisplayAlert:  called from main application thread, will block waiting for a response.

我不希望打印此邮件。有没有办法禁用它?或者,有更好的方法来解决这个问题吗?谢谢!

1 个答案:

答案 0 :(得分:3)

CFUserNotificationDisplayAlert()是一个便捷函数,它总是在等待用户输入时阻塞主线程。如果你不想阻止主线程,你必须自己创建CFUserNotification并将它附加到主线程的runloop:

// First, add member variables in your class to store the user notification and runloop source, like this.  You'll need to be able to access these variables later, from your callback method:
CFUserNotificationRef _userNotification;
CFRunLoopSourceRef _runLoopSource;

// When you want to show the alert, you will create it, create a runloop source for it, then attach the runloop source to the runloop:
_userNotification= CFUserNotificationCreate(... set this up the way you want to ...);
_runLoopSource = CFUserNotificationCreateRunLoopSource(NULL, userNotification, YourUserNotificationCallback, 0);
CFRunLoopAddSource(CFRunLoopGetMain(), runLoopSource, kCFRunLoopCommonModes);

// ...elsewhere, you'll need to define your callback function, something like this:
void YourUserNotificationCallback(CFUserNotificationRef userNotification, CFOptionFlags responseFlags)
{
    // Handle the user's input here.
    ...

    // Release your notification and runloop source:
    CFRunLoopRemoveSource(CFRunLoopGetMain(), _runLoopSource, kCFRunLoopCommonModes);
    CFRelease(_runLoopSource);
    CFRelease(_userNotification);
}