Search code examples
cocoansworkspace

Cocoa Unmounting drive but not ejecting it


Do you know you to unmount a drive without ejecting it. NSWorkspace has some methods to unmount drives but it also eject them.

Any idea ?


Solution

  • I am doing it as follows and it un-mounts the drive but doesn't eject it.

    (Actually I want to eject the disk, I can only un-mount the disk. :P Please share how to eject a disk.)

    DASessionRef session = DASessionCreate(kCFAllocatorDefault);
    
    CFURLRef path = CFURLCreateWithString(NULL, CFSTR("<path_to_your_volume_here>"), NULL);
    DADiskRef disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, path);
    
    DADiskUnmount(disk, kDADiskUnmountOptionDefault, __unmountCallback, NULL);
    

    This is the code I am still working on and is under development and testing.
    I am creating the "path" manually. You can use (and share) a better method to get the path of volume in a generic way. Perhaps this answer has hints of doing it the right way.

    I'll update when my development is refined and complete.