2012-03-22 23 views
12

MODIFICA: RISOLTO

Grazie Brooks. La tua domanda mi ha spinto a continuare a scavare se il file esistesse anche nel mio pacchetto - e così non è stato!Copia cartella (con contenuto) dal pacchetto alla directory Documenti - iOS

Quindi utilizzando questo codice (anche di seguito): iPhone/iPad: impossibile copiare la cartella da NSBundle a NSDocumentDirectory e le istruzioni per aggiungere una directory correttamente a Xcode (da here e di seguito) Sono riuscito a farlo funzionare .

cartella Copia in Xcode:

  1. creare una directory sul vostro Mac.
  2. Seleziona Aggiungi file esistenti al progetto
  3. selezionare la directory che si desidera importare
  4. Nella finestra pop-up assicuratevi di selezionare "Copia elementi nella cartella del gruppo di destinazione" e "creare riferimenti cartella per ogni cartelle aggiunte "
  5. " Aggiungi "
  6. La directory dovrebbe apparire blu anziché gialla.

    -(void) copyDirectory:(NSString *)directory { 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:directory]; 
    NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:directory]; 
    
    if (![fileManager fileExistsAtPath:documentDBFolderPath]) { 
        //Create Directory! 
        [fileManager createDirectoryAtPath:documentDBFolderPath withIntermediateDirectories:NO attributes:nil error:&error]; 
    } else { 
        NSLog(@"Directory exists! %@", documentDBFolderPath); 
    } 
    
    NSArray *fileList = [fileManager contentsOfDirectoryAtPath:resourceDBFolderPath error:&error]; 
    for (NSString *s in fileList) { 
        NSString *newFilePath = [documentDBFolderPath stringByAppendingPathComponent:s]; 
        NSString *oldFilePath = [resourceDBFolderPath stringByAppendingPathComponent:s]; 
        if (![fileManager fileExistsAtPath:newFilePath]) { 
         //File does not exist, copy it 
         [fileManager copyItemAtPath:oldFilePath toPath:newFilePath error:&error]; 
        } else { 
         NSLog(@"File exists: %@", newFilePath); 
        } 
    } 
    

    }

======================== FINE EDIT

Frus-stray- shee-on! Ad ogni modo ...

Il codice seguente copia correttamente la mia cartella dal pacchetto di app nella cartella Documenti del simulatore. Tuttavia sul dispositivo ottengo un errore e nessuna cartella. Usando ze Google ho scoperto che l'errore (260) significa che il file (in questo caso la mia cartella) non esiste.

Cosa potrebbe andare storto? Perché non posso copiare la mia cartella dal pacchetto in Documenti? Ho controllato che i file esistano, anche se la cartella non viene visualizzata, perché Xcode vuole file flat? Ha invece trasformato la mia cartella (trascinata in Xcode) in un file flat di risorse?

Grazie per l'assistenza.

// Could not copy report at path /var/mobile/Applications/3C3D7CF6-B1F0-4561-8AD7-A367C103F4D7/cmsdemo.app/plans.gallery to path /var/mobile/Applications/3C3D7CF6-B1F0-4561-8AD7-A367C103F4D7/Documents/plans.gallery. error Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x365090 {NSFilePath=/var/mobile/Applications/3C3D7CF6-B1F0-4561-8AD7-A367C103F4D7/cmsdemo.app/plans.gallery, NSUnderlyingError=0x365230 "The operation couldn’t be completed. No such file or directory"} 

NSString *resourceDBFolderPath; 

NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSError *error; 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:@"plans.gallery"]; 
BOOL success = [fileManager fileExistsAtPath:documentDBFolderPath]; 

if (success){ 
    NSLog(@"Success!"); 
    return; 
} else { 
    resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] 
             stringByAppendingPathComponent:@"plans.gallery"]; 
    [fileManager createDirectoryAtPath: documentDBFolderPath attributes:nil]; 
    //[fileManager createDirectoryAtURL:documentDBFolderPath withIntermediateDirectories:YES attributes:nil error:nil]; 

    [fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath   
          error:&error]; 
} 

    //check if destinationFolder exists 
if ([ fileManager fileExistsAtPath:documentDBFolderPath]) 
{ 
    //removing destination, so source may be copied 
    if (![fileManager removeItemAtPath:documentDBFolderPath error:&error]) 
    { 
     NSLog(@"Could not remove old files. Error:%@",error); 
     return; 
    } 
} 
error = nil; 
//copying destination 
if (!([ fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error ])) 
{ 
    NSLog(@"Could not copy report at path %@ to path %@. error %@",resourceDBFolderPath, documentDBFolderPath, error); 
    return ; 
} 
+1

questo: 'if (([Filemanager copyItemAtPath:! ResourceDBFolderPath topath: Errore documentDBFolderPath: & error ])) 'è illogico. Stai verificando se il metodo esiste, non se è riuscito. – CodaFi

risposta

10

Mi sono preso la libertà di modificare parte del codice che ritengo abbia bisogno di un po 'di pulizia. Stai usando metodi deprecati, metodi eccessivamente complicati e semplici e-ridicoli se-elis. Controllerei ovviamente che il percorso del file sia valido, non avendo idea di cosa sia un file .gallery e non tentando di fornire uno dummy, l'unico giudizio che posso fare è che il percorso del file sia semplicemente non valido perché la risorsa non lo fa esiste dove pensi che faccia (A un certo punto, si chiede di copiare il file nella directory di documenti, quindi verificare se esiste nel vostro pacco!)

-(void)testMethod { 

    NSString *resourceDBFolderPath; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                 NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:@"plans.gallery"]; 
    BOOL success = [fileManager fileExistsAtPath:documentDBFolderPath]; 

    if (success){ 
     NSLog(@"Success!"); 
     return; 
    } 
    else { 
     //simplified method with more common and helpful method 
     resourceDBFolderPath = [[NSBundle mainBundle] pathForResource:@"plans" ofType:@"gallery"]; 

     //fixed a deprecated method 
     [fileManager createDirectoryAtPath:documentDBFolderPath withIntermediateDirectories:NO attributes:nil error:nil]; 

     [fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath   
           error:&error]; 

     //check if destinationFolder exists 
     if ([ fileManager fileExistsAtPath:documentDBFolderPath]) 
     { 
      //FIXED, another method that doesn't return a boolean. check for error instead 
      if (error) 
      { 
       //NSLog first error from copyitemAtPath 
       NSLog(@"Could not remove old files. Error:%@", [error localizedDescription]); 

       //remove file path and NSLog error if it exists. 
       [fileManager removeItemAtPath:documentDBFolderPath error:&error]; 
       NSLog(@"Could not remove old files. Error:%@", [error localizedDescription]); 
       return; 
      } 
     } 
    } 
} 
+0

Apprezzo i commenti/le pulizie. – malaki1974

+1

Nessun problema! È ciò per cui vivo. – CodaFi

Problemi correlati