objective-c - 使用 NSWorkspace 通知对 MAC cocoa 应用程序进行 USB 检测

标签 objective-c c xcode

我正在尝试使用 NSworkspacenotification 方法实现 USB 检测,使用此代码

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    [[[NSWorkspace sharedWorkspace] notificationCenter]addObserver: [NotifTarget class] selector: @selector(mountNotification:)  name: NSWorkspaceDidMountNotification object: nil];
    [[[NSWorkspace sharedWorkspace] notificationCenter]
        addObserver: [NotifTarget class]
        selector: @selector(unmountNotification:)
        name: NSWorkspaceDidUnmountNotification     object: nil];
    [[NSRunLoop currentRunLoop] run];
    NSLog(@"Hello, World!");
    [pool drain];
    return 0;
}

此代码不检测 USB 和输出:

Hello, World!
Program ended with exit code: 0

这段代码有什么问题?

最佳答案

您尚未向运行循环添加任何输入源或计时器,因此它会立即退出。

关于objective-c - 使用 NSWorkspace 通知对 MAC cocoa 应用程序进行 USB 检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7545730/

相关文章:

ios - NSTimer 与 NSUserDefaults

objective-c - 自定义移动端Facebook登录按钮?

objective-c - iOS:在后台加载声音时出现多线程问题

c - c中的空函数

ios - 从信标确定 iDevice 的距离和方向

c - 在函数声明中收到错误

c - 从字符串读取关键字时重构 if 语句

ios - 如何在 ios 中动态地将一个屏幕移动到另一个屏幕?

ios - 在单独的 .swift 文件中使用扩展

swift - xcode 8 自动布局 uiimageview 问题