解決這個問題,建議在Apple Dev Forums,這裏是我想出了最後的功能:
- (NSURL *)volumeMountPathFromPath:(NSString *)path{
NSString *mountPath = nil;
NSString *testPath = [path copy];
while(![testPath isEqualToString:@"/"]){
NSURL *testUrl = [NSURL fileURLWithPath:testPath];
NSNumber *isVolumeKey;
[testUrl getResourceValue:&isVolumeKey forKey:NSURLIsVolumeKey error:nil];
if([isVolumeKey boolValue]){
mountPath = testPath;
break;
}
testPath = [testPath stringByDeletingLastPathComponent];
}
if(mountPath == nil){
return nil;
}
NSString *pathCompointents = [path substringFromIndex:[mountPath length]];
FSRef pathRef;
FSPathMakeRef((UInt8*)[path fileSystemRepresentation], &pathRef, NULL);
FSCatalogInfo catalogInfo;
OSErr osErr = FSGetCatalogInfo(&pathRef, kFSCatInfoVolume|kFSCatInfoParentDirID,
&catalogInfo, NULL, NULL, NULL);
FSVolumeRefNum volumeRefNum = 0;
if(osErr == noErr){
volumeRefNum = catalogInfo.volume;
}
CFURLRef serverLocation;
OSStatus result = FSCopyURLForVolume(volumeRefNum, &serverLocation);
if(result == noErr){
NSString *fullUrl = [NSString stringWithFormat:@"%@%@",
CFURLGetString(serverLocation), pathCompointents];
return [NSURL URLWithString:fullUrl];
}else{
NSLog(@"Error getting the mount path: %i", result);
}
return nil;
}
'FSCopyURLForVolume'實際上是給你卷的完整URL。如果您需要該捲上的某個項目的URL,則需要自行構建該項目,最好使用NSURL的方法或CFURL的功能。 – 2012-03-10 16:01:59