4

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?

Quinn Taylor
  • 44,553
  • 16
  • 113
  • 131
King
  • 239
  • 3
  • 17

2 Answers2

10

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.

stigi
  • 6,661
  • 3
  • 40
  • 50
  • 1
    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 Sep 11 '09 at 11:30
  • Make sure to make it a background app as well ;) – stigi Sep 11 '09 at 14:02
7

Use the DARegisterDiskDisappearedCallback function in the Disk Arbitration framework.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • It is described in Amit Singh's book "Mac OS X Internals: A Systems Approach" too. –  Sep 11 '09 at 12:17
  • Documentation can now be found at http://developer.apple.com/documentation/Darwin/Reference/DiscArbitrationFramework/ – Quinn Taylor Dec 30 '11 at 14:54
  • @QuinnTaylor: Documentation links added. Thanks! – Peter Hosey Dec 30 '11 at 16:37
  • 1
    @PacMan--: Sort of. There are certain non-trivial cases in which a volume can be unmounted without disappearing (particularly when it's not the only volume on that disk). You also want DescriptionChanged; a volume whose disk description doesn't have a mount path is not mounted. – Peter Hosey Mar 19 '14 at 06:07
  • Absolutely. I'd like to rephrase my comment: Using the Disk Arbitration framework would probably be the correct way to do it; it gives you many possibilities, when comparing to other solutions. (as for myself, I just used the DA framework and I supply a matchDictionary, so I filter out most harddisks - eg. matching for "DADeviceProtocol" = "USB" and "DAMediaRemovable" = 1) –  Mar 19 '14 at 21:38