Reputation: 239
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?
Upvotes: 4
Views: 2150
Reputation: 6711
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.
Upvotes: 10
Reputation: 96333
Use the DARegisterDiskDisappearedCallback
function in the Disk Arbitration framework.
Upvotes: 7