完整的URL从FSCopyURLForVolume
我遇到一个问题,获取文件的完整url与FSCopyURLForVolume
。我正在使用这个问题的代码Determine AFP share from a file URL,但它没有给我完整的网址。例如:完整的URL从FSCopyURLForVolume
有了这样的路径:/Volume/server/html/index.html
所有我得到的回复是URL到基座安装架:nfs://real_server_name/vol
叶目录和文件名都不放过,全路径是可用文件信息,所以必须有一种方法来获取这些信息。
编辑:
后一些更多的挖是好像我要使用kFSCatInfoParentDirID
和kFSCatInfoNodeID
获取父和节点(文件)的ID,但我不知道如何把它变成有用的东西。
解决这个问题,建议在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;
}
你为什么使用'stringWithFormat:'?您可以创建一个相对于另一个URL的URL。 – 2012-03-10 16:00:00
因为FSPathMakeRef,FSGetCatalogInfo和FSCopyURLForVolume是从Mac OS X 10.8弃用我现代化的代码获取Mac OS X挂载卷上的UNC网络路径。
NSError *error=nil; //Error
NSURL *volumePath=nil; //result of UNC network mounting path
NSString* testPath [email protected]"/Volumes/SCAMBIO/testreport.exe"; //File path to test
NSURL *testUrl = [NSURL fileURLWithPath:testPath]; //Create a NSURL from file path
NSString* mountPath = [testPath stringByDeletingLastPathComponent]; //Get only mounted volume part i.e. /Volumes/SCAMBIO
NSString* pathComponents = [testPath substringFromIndex:[mountPath length]]; //Get the rest of the path starting after the mounted path i.e. /testereport.exe
[testUrl getResourceValue:&volumePath forKey:NSURLVolumeURLForRemountingKey error:&error]; //Get real UNC network mounted path i.e. smb://....
NSLog(@"Path: %@%@", volumePath,pathComponents); //Write result to debug console
结果是在我的情况, 路径:SMB://[email protected]/SCAMBIO/testreport.exe
您需要指定网络映射的卷。
ciao。
'FSCopyURLForVolume'实际上是给你卷的完整URL。如果您需要该卷上的某个项目的URL,则需要自行构建该项目,最好使用NSURL的方法或CFURL的功能。 – 2012-03-10 16:01:59