I am want to unmount a disk WITHOUT EJECTING. To do that I tried following code
{
NSString *path;
CFStringRef *volumeName=(__bridge CFStringRef)path;
DASessionRef session = DASessionCreate(kCFAllocatorDefault);
CFURLRef pathRef = CFURLCreateWithString(NULL, CFSTR("/volumes/Untitled"), NULL);
DADiskRef disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, pathRef);
DADiskUnmount(disk, kDADiskUnmountOptionForce, NULL, NULL);
}
This code is from this question, Thanks to @zeFree
Its working but I want dynamic path to the volume where as in the code its static. I tried changing NSString to CFStringRef and then tried to use at place of path("/volumes/Untitled") mention but its still same.
Any suggestion is welcome.
First of all, you are strongly discouraged from using kDADiskUnmountOptionForce
.
This is a method to unmount a volume at given URL with basic error handling and memory management.
- (BOOL)unmountVolumeAtURL:(NSURL *)url
BOOL returnValue = NO;
DASessionRef session = DASessionCreate(kCFAllocatorDefault);
if (session) {
DADiskRef disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, (__bridge CFURLRef)url);
if (disk) {
DADiskUnmount(disk, kDADiskUnmountOptionDefault, NULL , NULL);
returnValue = YES;
CFRelease(disk);
} else {
NSLog(@"Could't create disk reference from %@", url.path);
}
} else {
NSLog(@"Could't create DiskArbritation session");
}
if (session) CFRelease(session);
return returnValue;
}
The error handling could be still improved by providing a callback handler in the DADiskUnmount
function.