CFUserNotificationDisplayAlert

CFUserNotificationDisplayAlert

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

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


我不希望打印此消息。有什么办法可以禁用它吗?还是有更好的方法来解决这个问题?谢谢!

最佳答案

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);
}

关于objective-c - 如何抑制CFUserNotificationDisplayAlert生成的控制台消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4116702/

10-13 04:02