这应该是如此简单,但我正在努力解决它。我想要做的就是将NSURL文件从其位置复制到我的应用程序文档目录。我不想重命名该文件,并希望保留相同的名称。请注意,此文件来自电子邮件附件。
这是我到目前为止的代码
-(BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
if (url != nil && [url isFileURL])
{
NSLog(@"url: %@ ...", url);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask ,YES);
NSString* documentsPath = [paths objectAtIndex:0];
//Not sure what to do here. I could parse URL and get the last string out of it
// which is the file name but I am not sure that's easiest way to do things.
//[fileManager copyItemAtURL:url toURL:backupDir error:&error];
NSLog(@"Email Copy Error: %@ ...", error);
}
return YES;
}
file:///private/var/mobile/Applications/1401045C-8738-458A-954A-EC30729626E1/Documents/Inbox/almonte-2.ttf ...
答案 0 :(得分:6)
我认为你已经走在了正确的道路上(!)。创建目标路径 来自目标目录和url的最后一个路径组件:
NSString *destPath = [documentsPath stringByAppendingPathComponent:[url lastPathComponent]];
并将文件从源复制到目标:
[fileManager copyItemAtPath:[url path] toPath:destPath error:&error];