繁体   English   中英

将所有文件从文档/收件箱移到iOS应用中的文档

[英]Moving all files from documents/inbox to documents in iOS app

我有一个iOS应用程序,并且已经为我的.plist文件实现了“打开方式”功能。 一切工作正常,只是当我导入.plist文件时,它们最终出现在document / inbox中,而不是在常规documents文件夹中。 有没有办法更改它以使其显示在documents文件夹中而不是在收件箱文件夹中,或者移动它们? 我当前正在使用以下代码,但似乎没有做任何事情。 该代码来自页面上的第3条回复。

//get to inbox directory
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray *inboxContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSString stringWithFormat:@";%@/Inbox", documentsDirectory] error:nil];

//move all the files over
for (int i = 0; i != [inboxContents count]; i++) {
    NSString *oldPath = [NSString stringWithFormat:@";%@/Inbox/@%", documentsDirectory, [inboxContents objectAtIndex:i]];
    NSString *newPath = [NSString stringWithFormat:@";%@", documentsDirectory, [inboxContents objectAtIndex:i]];
    [[NSFileManager defaultManager] moveItemAtPath:oldPath toPath:newPath error:nil];
}

如果有可能,我还想在所有文件传输完毕后清除收件箱文件夹,但这不是优先事项,因为plist文件往往很小。

编辑:我发现(根据用户originaluser2的使用断点的建议)我的应用程序未正确选择目录,因此我更改了代码,使其选择了预设的字符串。 这是viewDidLoad中的当前代码

//Turn every file inside the directory into an array
// Note to self: remember to actually put files in the Documents folder. Use the code in the apparopriately marked file
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//strings to actually get the directories
NSString *appFolderPath = [path objectAtIndex:0];
NSString *inboxAppFolderPath = [appFolderPath stringByAppendingString:@"/Inbox"]; //add ".plist" to the end of the recipe name


//predicates to hide unwanted files 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"not SELF beginswith[c] '.DS_Store'"];
NSPredicate *inboxPredicate = [NSPredicate predicateWithFormat:@"not SELF beginswith[c] 'Inbox'"];
recipes = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:appFolderPath error:nil];
recipes = [recipes filteredArrayUsingPredicate:predicate];
recipes = [recipes filteredArrayUsingPredicate:inboxPredicate];




//move all files from inbox to documents root
//get to inbox directory
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSArray *inboxContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSString stringWithFormat:inboxAppFolderPath, documentsDirectory] error:nil];

//move all the files over
for (int i = 0; i != [inboxContents count]; i++) {
    NSString *oldPath = [NSString stringWithFormat:inboxAppFolderPath, documentsDirectory, [inboxContents objectAtIndex:i]];
    NSString *newPath = [NSString stringWithFormat:appFolderPath, documentsDirectory, [inboxContents objectAtIndex:i]];
    [[NSFileManager defaultManager] moveItemAtPath:oldPath toPath:newPath error:nil];
}
//end of moving all files

使用此代码,该应用程序可以正确查看目录,并可以告诉我哪些文件位于“收件箱”文件夹中,但实际上并未将其内容转移到“文档”文件夹中。

问题在于您定义的路径:

NSString *oldPath = [NSString stringWithFormat:inboxAppFolderPath, documentsDirectory, [inboxContents objectAtIndex:i]];
NSString *newPath = [NSString stringWithFormat:appFolderPath, documentsDirectory, [inboxContents objectAtIndex:i]];

moveItemAtPath:方法提供NSError后,它返回错误:

2016-01-19 08:43:57.534 Moving Files[35505:9385200] error: “Inbox” couldn’t be moved to “E70C5B67-D502-4C06-B480-03675E35E999” because an item with the same name already exists.

发生的情况是您的字符串格式不正确,因为它仅采用了您提供的第一个参数,即Inbox文件夹路径(而不是Inbox文件夹中的文件路径)。

您只想将路径定义更改为以下内容:

NSString *oldPath = [NSString stringWithFormat:@"%@/%@", inboxAppFolderPath, [inboxContents objectAtIndex:i]];
NSString *newPath = [NSString stringWithFormat:@"%@/%@", appFolderPath, [inboxContents objectAtIndex:i]];

那应该解决问题。

请记住,将来,如果您在使用NSFileManager遇到问题,请始终尝试从中获取错误:

NSError* error;
[[NSFileManager defaultManager] moveItemAtPath:oldPath toPath:newPath error:&error];
NSLog(@"error: %@", error.localizedDescription);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM