簡體   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