将UIDocument移动到文件系统上的新位置的正确方法是什么? [英] What is the proper way to move a UIDocument to a new location on the file-system
问题描述
我有一个基于文档的iOS应用程序,它包含一个 UIDocument
子类。我需要能够移动它在文件系统上表示的文件。我曾考虑将它移动到 NSFileManager
,但是这会弄乱 UIDocument
的 fileURL
属性。关于如何解决这个小难题的任何想法?解决方案
处理程序,使用NSFileManager移动文件。一旦文件被成功移动,使用新的文件URL初始化你的UIDocument子类的一个新实例:
NSURL * directoryURL = [_document.fileURL URLByDeletingLastPathComponent];
NSFileManager * fileManager = [[NSFileManager alloc] init];
NSString * filePath = [directoryURL.path stringByAppendingPathComponent:@NewFileName];
[_document closeWithCompletionHandler:^(BOOL success){
NSError * error;
if(success)
success = [fileManager moveItemAtPath:_document.fileURL.path toPath:filePath error:& error];
if(success){
NSURL * url = [NSURL fileURLWithPath:filePath];
//我打开文档并更新setDocument中的UI:
self.document = [[MyDocumentSubclass alloc] initWithFileName:[url lastPathComponent] dateModified:[NSDate date] andURL:url ]。
} else {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:nil message:@无法重命名文档委托:无cancelButtonTitle:@DismissotherButtonTitles:nil];
[alert show];
NSLog(@无法移动文档:%@,错误);
}
}];
I have a document-based iOS application with a UIDocument
subclass. I need to be able to move the file it represents on the file-system. I have considered moving it with an NSFileManager
, but this would mess up the UIDocument
's fileURL
property. Any ideas on how to solve this little conundrum?
You can rename a UIDocument by first closing it, and then in the completion handler, moving the file using NSFileManager. Once the file has been successfully moved, initialize a new instance of your UIDocument subclass using the new file URL:
NSURL *directoryURL = [_document.fileURL URLByDeletingLastPathComponent];
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString *filePath = [directoryURL.path stringByAppendingPathComponent:@"NewFileName"];
[_document closeWithCompletionHandler:^(BOOL success) {
NSError *error;
if (success)
success = [fileManager moveItemAtPath:_document.fileURL.path toPath:filePath error:&error];
if (success) {
NSURL *url = [NSURL fileURLWithPath:filePath];
// I handle opening the document and updating the UI in setDocument:
self.document = [[MyDocumentSubclass alloc] initWithFileName:[url lastPathComponent] dateModified:[NSDate date] andURL:url];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Unable to rename document" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[alert show];
NSLog(@"Unable to move document: %@", error);
}
}];
这篇关于将UIDocument移动到文件系统上的新位置的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!