views:

244

answers:

2

Hi Guys

I am working on an app which should detect events that happen when removable storage is unmounted or forcefully unplugged from the USB. how should I get 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. please can u guys write or point me to some sample code to understand how these events are trapped ?

Thanks

+2  A: 

i am working on a app which should detect events that happen when removable storage is unmounted or forcefully unplugged from the USB. how should i get these events?

Use the DARegisterDiskDisappeared function in the Disk Arbitration framework. There's no HTML documentation; all the documentation is in the headers.

Peter Hosey
It is described in Amit Singh's book "Mac OS X Internals: A Systems Approach" too.
Graham Lee
+1  A: 

Hello,

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

stigi
Man, and I work on HardwareGrowler! I would not make those class methods, though. Someday, when other things aren't so pressing, I intend to go through and change the notifiers to instances (among other clean-ups).
Peter Hosey
Make sure to make it a background app as well ;)
stigi