簡體   English   中英

無法將文件夾從應用程序包復制到文檔目錄

[英]Unable to copy folder from application bundle to document directory

我正在嘗試將文件夾從應用程序包復制到iphone文檔目錄,但它沒有發生。

我用過這個代碼

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Photos"];


    NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths1 objectAtIndex:0];

    NSError *error1;
    NSArray *resContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:sourcePath error: &error1];
    if (error1) {
        NSLog(@"error1 : %@",error1);
    }
    [resContents enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
     {
         NSError* error;
         if (![[NSFileManager defaultManager]
               copyItemAtPath:[sourcePath stringByAppendingPathComponent:obj]
               toPath:[documentsDirectory stringByAppendingPathComponent:obj]
               error:&error])
             NSLog(@"%@", [error localizedDescription]);
     }];
}

並嘗試了這種方法

- (void)viewDidLoad{
     [self copyFolder];
    }

- (void) copyFolder {
    BOOL success1;
    NSFileManager *fileManager1 = [NSFileManager defaultManager];
    fileManager1.delegate = self;
    NSError *error1;
    NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory1 = [paths1 objectAtIndex:0];
    NSString *writableDBPath1 = [documentsDirectory1 stringByAppendingPathComponent:@"/Photos"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:writableDBPath1])
        [[NSFileManager defaultManager] createDirectoryAtPath:writableDBPath1 withIntermediateDirectories:NO attributes:nil error:&error1];

    NSString *defaultDBPath1 = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Photos"];
    NSLog(@"default path %@",defaultDBPath1);
    success1 = [fileManager1 copyItemAtPath:defaultDBPath1 toPath:writableDBPath1 error:&error1];
    if (!success1 )
    {
        NSLog(@"error1 : %@",error1);
    } else {
    }
}

- (BOOL)fileManager:(NSFileManager *)fileManager shouldProceedAfterError:(NSError *)error copyingItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath{
    if ([error code] == 516) //error code for: The operation couldn’t be completed. File exists
        return YES;
    else
        return NO;
}

它顯示此錯誤

錯誤域= NSCocoaErrorDomain代碼= 260“操作無法完成。(Cocoa錯誤260.)”UserInfo = 0x8a39800 {NSUnderlyingError = 0x8a385b0“操作無法完成。沒有這樣的文件或目錄”,NSFilePath = / Users / user / Library / Application Support / iPhone Simulator / 7.0.3 / Applications / FC7F93EE-CFB7-41CF-975B-2E3B18760202 / app.app / Photos,NSUserStringVariant =(Folder)}

但這是文件夾

在此輸入圖像描述

照片看起來像一個組而不是文件夾引用。 因此,所有圖像photo1.jpg ..等都直接存儲在您的資源包下。

路徑<resource_bundle>/Photos不存在。 要驗證這一點,請轉到應用程序.app

.app->Right-click->Show Package Contents

不要將Photos添加為組,而是將其添加為文件夾引用,並在目標Copy Bundle Resources下,確保添加整個文件夾。

希望有所幫助!

您的照片不在照片目錄中。 它們只存在於/Users/devubhamanek/Library/Application Support/iPhone Simulator/7.0.3/Applications/FC7F93EE-CFB7-41CF-975B-2E3B18760202/MTPhotoViewer.app/ like /Users/devubhamanek/Library/Application Support/iPhone Simulator/7.0.3/Applications/FC7F93EE-CFB7-41CF-975B-2E3B18760202/MTPhotoViewer.app/photo1.jpg

如果要在bundle中創建目錄,則應在Photos目錄中添加如下文件: 在此輸入圖像描述

項目導航器中的文件夾應為: 在此輸入圖像描述

確保您的文件夾存在。 您可以輕松地將XCode的組分配到物理文件夾

單擊group ,您將在右側窗格中找到位置設置。

folder標志選擇物理文件夾location

實際上,這對於保持項目結構清潔(不僅在XCode項目中)而且在物理上在磁盤上非常有用。

在此輸入圖像描述

Swift 3完整解決方案代碼注意:創建文件夾后參考使用以下方法。

func copyFolderFromThisAppMainToUserDocument(folderName: String) -> Bool {

    let fileMgr = FileManager.default
    let userDocumentURL = fileMgr.urls(for: .documentDirectory, in: .userDomainMask).first!
    let urlForCopy = userDocumentURL.appendingPathComponent(folderName, isDirectory: true)
    if let bundleURL = Bundle.main.url(forResource: folderName, withExtension: "") {
        // if exist we will copy it
        do {
            try fileMgr.copyItem(at: bundleURL, to: urlForCopy)
            return true
        } catch let error as NSError { // Handle the error
            print("\(folderName) copy failed! Error:\(error.localizedDescription)")
            return false
        }
    } else {
        print("\(folderName) not exist in bundle folder")
        return false
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM