我们还可以通过将kIOUSBDeviceClassName与IOServiceMatching和IOServicesGetMatchingServices一起使用来了解所有USB设备即使是带有两个函数的kIOMediaClassName,我们也会知道卷媒体,我们可以通过路径确定每个卷媒体属于哪个USB设备.但是我不知道卷媒体的挂载点.还有其他有用的东西.对不起,我的泳池英语.解决方案另一种方式.使用kIOMediaClass创建匹配字典 matchingDict = IOServiceMatching(kIOMediaClass); 如果您只想获取可移动存储量使用kIOMediaRemovableKey和kCFBooleanTrue设置字典 CFDictionarySetValue(matchingDict,CFSTR(kIOMediaRemovableKey),kCFBooleanTrue); 并立即获得匹配服务 IOServiceGetMatchingService(kIOMasterPortDefault,matchingDict,& iterator); 您现在可以枚举设备. while(((removableMedia = IOteratorNext(iterator)))){IORegistryEntryGetName(removableMedia,deviceName);//和您可以做的其他事情kr = IORegistryGetPath(removableMedia,kIOServicePlane,devicePath);//将路径与您在设备中获得的路径进行比较.//如果一个设备的路径是此媒体的子字符串//我们可以简单地认为该媒体属于该设备//您可以通过以下代码获取挂载点DASessionRef sessionRef = DASessionCreate(kCFAllocatorDefault);如果(sessionRef){DADiskRef diskRef-DADiskCreateFromIOMedia(kCFAllocatorDefault,sessionRef,removableMedia);如果(diskRef){CFDictionaryRef * diskProperty = DADisCopyDescription(diskRef);如果(属性){NSURL * mountURL = [(NSDictionary *)属性objectForKey:(NSString *)kDADiskDescriptionVolumePathKey];//mountURL或[mountURL路径]是您想要的安装点CFRelease(diskProperty);}CFRelease(diskRef);}CFRelease(sessionRef);}//不要忘记释放IOObjectRelease(removableMedia);} 您可以观察到如下所示的观察器安装/卸载事件 [[[[NSWorkSpace sharedWorkspace]通知中心] addObsever:自我选择器:@选择器(volumeMounted :)名称:NSWorkspaceDidMountNotification对象:无];[[[[NSWorkSpace sharedWorkspace]通知中心] addObsever:自我选择器:@selector(volumeUnmount :)名称:NSWorkspaceDidUnmountNotification对象:无]; for example, i have a flash disk(KingStone Mass Storage), and only one partition , so when I plug it on mac. I'll see a Volume(it might be /Volumes/KingStone) was mounted automatically. we could see volume(/Volumes/Kingstone) is belong to the KingSton disk.but now I pluged another disk, such as AData disk. and another volume was mounted. and how could I know which volume is belong to kingstone disk.(we could know which disk is kongston by VenderID).now in code, we could know mounted volumes by call [[NSWorkspace sharedWorkspace] mountedRemovableMedia] OR [[NSFileManager defaultFileManager] mountedVolumeURLsInclud.....]we could also know all usb device by using kIOUSBDeviceClassName with IOServiceMatching and IOServicesGetMatchingServiceseven kIOMediaClassName with the two function we will know volume media,we could determine every volume media belongs to which usb device by path.but I don't know the mount point of volume media.either something else useful.sorry for my pool English. 解决方案 Another way.Create a Matching Dictionary with kIOMediaClassmatchingDict = IOServiceMatching(kIOMediaClass);if you only want to get removable storage volumeset the dictionary with kIOMediaRemovableKey and kCFBooleanTrueCFDictionarySetValue(matchingDict, CFSTR(kIOMediaRemovableKey), kCFBooleanTrue);and getting matching service now,IOServiceGetMatchingService(kIOMasterPortDefault, matchingDict, &iterator);you can enumerate your device now.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);}and you could Observer mount/unmount event like below[[[NSWorkSpace sharedWorkspace] notificationCenter] addObsever:self selector:@selector(volumeMounted:) name:NSWorkspaceDidMountNotification object:nil];[[[NSWorkSpace sharedWorkspace] notificationCenter] addObsever:self selector:@selector(volumeUnmounted:) name:NSWorkspaceDidUnmountNotification object:nil]; 这篇关于我怎么知道Mac上USB设备节点和音量之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
06-19 19:37