我正试图卸载OSX中的磁盘。代码工作正常,但只有当磁盘卸载成功时,才会出现错误时才调用回调。我读了DiskArbitrationProgGuide并遵循了这些步骤,但还没有进展。有人能帮我吗?
@interface DriverUtilitiesController()
void unmount_done(DADiskRef disk,
DADissenterRef dissenter,
void *context);
@end
+ (void)umnountDrivePath:(NSString *)voulumePath
{
DASessionRef session = DASessionCreate(kCFAllocatorDefault);
CFURLRef path = CFURLCreateWithString(NULL, (__bridge CFStringRef)voulumePath, NULL);
DADiskRef disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, session, path);
DADiskUnmount(disk, kDADiskUnmountOptionDefault, unmount_done, NULL);
CFRelease(disk);
}
#pragma mark - Unmount Callback
void unmount_done(DADiskRef disk,
DADissenterRef dissenter,
void *context)
{
NSLog(@"Inside unmount_done");
if (dissenter)
{
// Unmount failed. //
NSLog(@"Unmount failed.");
} else {
NSLog(@"Unmounted Volume");
}
}更新.由于Ken的帮助,代码现在可以工作了
- (id)init
{
self = [super init];
self.session = DASessionCreate(kCFAllocatorDefault);
DASessionScheduleWithRunLoop(_session, [[NSRunLoop mainRunLoop] getCFRunLoop], kCFRunLoopDefaultMode);
return self;
}
- (void)umnountDrivePath:(NSString *)voulumePath
{
CFURLRef path = CFURLCreateWithString(NULL, (__bridge CFStringRef)voulumePath, NULL);
DADiskRef disk = DADiskCreateFromVolumePath(kCFAllocatorDefault, self.session, path);
DADiskUnmount(disk, kDADiskUnmountOptionDefault, unmount_done, (__bridge void *)(self));
CFRelease(disk);
}
void unmount_done(DADiskRef disk,
DADissenterRef dissenter,
void *context)
{
if (dissenter)
{
// Unmount failed. //
NSLog(@"Unmount failed.");
} else {
NSLog(@"Unmounted Volume");
}
DriverUtilitiesController *driverUtilitiesController = (__bridge DriverUtilitiesController *)context;
[driverUtilitiesController clearUnmountCallback];
}
- (void)clearUnmountCallback
{
DASessionUnscheduleFromRunLoop(_session, [[NSRunLoop mainRunLoop] getCFRunLoop], kCFRunLoopDefaultMode);
CFRelease(self.session);
}发布于 2016-01-01 05:09:16
DADiskUnmount()异步操作。当函数返回到您的代码时,磁盘不一定已被卸载。如果它成功了,它可能会在以后的某个时候发生。到时我们会打电话给你的。
程序等待该事件并响应调用回调的机制要么是运行循环,要么是调度队列。会话对象负责管理此等待和调用。您需要将会话对象安排在运行循环或调度队列上。您可以使用DASessionScheduleWithRunLoop()或DASessionSetDispatchQueue(),如磁盘仲裁编程指南:使用磁盘仲裁通知和批准回调-调度运行循环或调度队列的会话中所述。
这意味着您不希望为每次卸载磁盘的尝试创建一个新的session对象。此外,您希望保持对session对象的引用,以便在不再需要它时(在您不再需要从它获得回调之后)取消调度并释放它。
https://stackoverflow.com/questions/34552164
复制相似问题