其他方式。
使用 kIOMediaClass 创建匹配字典
matchingDict = IOServiceMatching(kIOMediaClass);
如果您只想获取可移动存储卷,请使用 kIOMediaRemovableKey 和 kCFBooleanTrue 设置字典
CFDictionarySetValue(matchingDict, CFSTR(kIOMediaRemovableKey), kCFBooleanTrue);
并立即获得匹配服务,
IOServiceGetMatchingService(kIOMasterPortDefault, matchingDict, &iterator);
您现在可以枚举您的设备。
while((removableMedia = IOteratorNext(iterator)))
{
IORegistryEntryGetName(removableMedia, deviceName);
// and something else you can do
kr = IORegistryGetPath(removableMedia, kIOServicePlane, devicePath);
// compare the path with path you get in device.
// if one device's path is the substring of this media
// we could simply think this media is belong to the device
// you could get mount point by following code
DASessionRef sessionRef = DASessionCreate(kCFAllocatorDefault);
if (sessionRef) {
DADiskRef diskRef - DADiskCreateFromIOMedia(kCFAllocatorDefault, sessionRef, removableMedia);
if (diskRef) {
CFDictionaryRef *diskProperty=DADisCopyDescription(diskRef);
if (property) {
NSURL *mountURL = [(NSDictionary*)property objectForKey:(NSString*)kDADiskDescriptionVolumePathKey];
// mountURL or [mountURL path] is the mount point you want
CFRelease(diskProperty);
}
CFRelease(diskRef);
}
CFRelease(sessionRef);
}
// don't forget to release
IOObjectRelease(removableMedia);
}
你可以像下面这样观察挂载/卸载事件
[[[NSWorkSpace sharedWorkspace] notificationCenter] addObsever:self selector:@selector(volumeMounted:) name:NSWorkspaceDidMountNotification object:nil];
[[[NSWorkSpace sharedWorkspace] notificationCenter] addObsever:self selector:@selector(volumeUnmounted:) name:NSWorkspaceDidUnmountNotification object:nil];