Detect when removable storage is unmounted

I am working on an application that should detect events that occur when removable storage is unmounted or forcibly disconnected from USB. How can I get these events?

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

+3
source share
2 answers

Copy 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];

Then you need to implement methods to respond to ala notifications

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

http://growl.info/source.php /HardwareGrowler VolumeNotifier.h/m

UPDATE:

Peters . , .

+10

Source: https://habr.com/ru/post/1717390/


All Articles