在OS X中检测已安装的USB设备

时间:2015-02-27 10:53:10

标签: macos usb

所以我尝试在我的Mac Mini上检测OS X中的已安装和未安装的USB设备

我已经按照其他指南进行了操作,但似乎我没有收到任何通知。

这是我的代码,我也尝试将其置于视图控制器类中无济于事。

@implementation AppDelegate

- (void)deviceMounted: (NSNotification *) notification
{
    NSLog(@"Mounted");
}
- (void)deviceUnmounted: (NSNotification *) notification
{
    NSLog(@"Unmounted");
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
   NSNotificationCenter *notificationCenter2 = [[NSWorkspace sharedWorkspace] notificationCenter];

    // Notification for Mountingthe USB device
    [notificationCenter2 addObserver:self selector:@selector(deviceMounted:)  name:NSWorkspaceDidMountNotification object:nil];

    // Notification for Un-Mountingthe USB device
    [notificationCenter2 addObserver:self selector:@selector(deviceUnmounted:)  name:NSWorkspaceDidUnmountNotification object:nil];
}

1 个答案:

答案 0 :(得分:0)

您的代码尝试记录的方式不正确,NSLog就是您所需要的:

- (void)deviceMounted: (NSNotification *) notification
{
    NSLog(@"Mounted");
}
- (void)deviceUnmounted: (NSNotification *) notification
{
    NSLog(@"Unmounted");
}

<强>结果

2015-02-27 04:07:00.593 notify[32431:16256290] Mounted
2015-02-27 04:07:11.587 notify[32431:16256290] Unmounted