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

时间:2016-01-17 04:47:26

标签: ios objective-c directory

我有一个iOS应用程序,我已经实现了" Open in"函数进入我的应用程序的.plist文件。一切都很完美,除了当我导入.plist文件时,它们最终出现在文档/收件箱中而不是常规文档文件夹中。有没有办法将其更改为显示在文档文件夹而不是收件箱文件夹中,还是移动它们?我目前正在使用以下代码,但它似乎没有做任何事情。该代码来自this页面上的第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

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

1 个答案:

答案 0 :(得分:1)

问题在于您定义的路径:

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

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

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