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



我有一个iOS应用程序,我已经在我的应用程序中为.plist文件实现了"打开方式"功能。一切都运行良好,除了当我导入 .plist 文件时,它们最终出现在文档/收件箱中,而不是在常规文档文件夹中。有没有办法将其更改为显示在文档文件夹中而不是收件箱文件夹中,或者移动它们?我目前正在使用以下代码,但它似乎没有做任何事情。该代码来自此页面上的第三个回复。

//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 建议使用断点)我的应用程序没有正确选取目录,所以我更改了代码,以便它拾取预设的字符串。这是视图中的当前代码DidLoad

//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.

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

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

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);

最新更新