Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify a mounted device as removable?

How can I figure out if a mounted device can be unmounted/ejected ?

I tried to get all devices with the help of the NSFileManager.

NSArray *keys = [NSArray arrayWithObjects:NSURLVolumeNameKey, NSURLVolumeURLForRemountingKey, NSURLVolumeIsEjectableKey, NSURLVolumeIsRemovableKey, nil];
NSArray *urls = [[NSFileManager defaultManager] mountedVolumeURLsIncludingResourceValuesForKeys:keys options:0];

for (NSURL *url in urls) 
{
NSError *error;
NSNumber *isRemovable;
NSString *volumeName; 

[url getResourceValue:&isRemovable forKey:NSURLVolumeIsRemovableKey error:&error];
[url getResourceValue:&volumeName forKey:NSURLVolumeNameKey error:&error];

NSLog(@"Standard Device : %@ - Removable: %c", volumeName, [isRemovable boolValue]);

}

But when I iterate through the list, I always get this e.g.:

Standard Device Name: My Book - isEjectable:

FYI, "My Book" is a USB drive.

I hope someone can point out what is wrong.

Thanks

like image 730
elementsense Avatar asked Jan 25 '26 03:01

elementsense


1 Answers

You want the NSURLVolumeIsEjectableKey not NSURLVolumeIsRemovableKey.

NSURLVolumeIsRemovableKey means the media can be physically removed from the device. NSURLVolumeIsEjectableKey indicates that the volume can be unmounted.

like image 153
bbum Avatar answered Jan 27 '26 01:01

bbum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!