所以我尝试在我的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];
}
答案 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