gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:33:53 25 4
gpt4 key购买 nike

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

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

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

最佳答案

CFUserNotificationDisplayAlert() 是一个方便的函数,在等待用户输入时始终阻塞主线程。如果您不想阻塞主线程,则必须自己创建 CFUserNotification 并将其附加到主线程的运行循环:

// 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com