4

我正在开发一个应用程序,该应用程序应该检测当可移动存储被卸载或从 USB 中强行拔出时发生的事件。我怎样才能收到这些事件?

我已经看到NSWorkspace了顺利卸载设备的第一种可能性,但是这个类有卸载设备之类的方法-unmountAndEjectDeviceAtPath:。有人可以指出一些检测未安装卷的示例代码吗?

4

2 回答 2

10

HardwareGrowler 的一段代码:

NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
NSNotificationCenter *center = [workspace notificationCenter];

[center addObserver:[VolumeNotifier class] selector:@selector(volumeDidMount:) name:NSWorkspaceDidMountNotification object:nil];
[center addObserver:[VolumeNotifier class] selector:@selector(volumeDidUnmount:) name:NSWorkspaceDidUnmountNotification object:nil];
[center addObserver:[VolumeNotifier class] selector:@selector(volumeWillUnmount:) name:NSWorkspaceWillUnmountNotification object:nil];

然后,您需要实现对通知 ala 做出反应的方法

+ (void) volumeDidUnmount:(NSNotification *)aNotification;
{
...
}

对于整个实现检查http://growl.info/source.php 在源包中转到 Extras/HardwareGrowler 并在那里检查VolumeNotifier.h/m

更新:

彼得斯的回答优于这个。如果您遇到此问题,请考虑使用磁盘仲裁框架。

于 2009-09-11T10:30:45.117 回答
7

使用磁盘仲裁框架DARegisterDiskDisappearedCallback功能

于 2009-09-11T03:29:52.460 回答