检测可移动存储何时卸载

发布于 2024-08-04 06:18:24 字数 205 浏览 14 评论 0 原文

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

我已经看到 NSWorkspace 是平滑卸载设备的第一种可能性,但此类具有诸如 -unmountAndEjectDeviceAtPath: 之类的方法来卸载设备。有人可以向我指出一些检测未安装卷的示例代码吗?

I am working on an app which should detect events that happen when removable storage is unmounted or forcefully unplugged from the USB. How can I receive these events?

I have seen NSWorkspace for the first possibility of smoothly unmounting the device but this class has methods like -unmountAndEjectDeviceAtPath: to unmount a device. Can someone point me to some sample code that detects unmounted volumes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻梦旅人 2024-08-11 06:18:24

来自 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];

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

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

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

更新:

Peters 的答案优于此。如果遇到此问题,请考虑使用磁盘仲裁框架。

A pice of code from 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];

You then need to implement the methods to react on the notifications ala

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

For the whole implementation check out http://growl.info/source.php
In the Source bundle go to Extras/HardwareGrowler and there check out VolumeNotifier.h/m

UPDATE:

Peters answer is superior to this. Please consider using the Disk Arbitration framework if you come about this problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文