使用NSWorkspace通知在MAC cocoa应用程序上进行USB检测

时间:2011-09-25 13:17:32

标签: 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

这段代码出了什么问题?

1 个答案:

答案 0 :(得分:1)

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