Unmounting Drive/volume without ejecting

与世无争的帅哥 提交于 2019-12-03 20:54:37

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!