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:
- creare una directory sul vostro Mac.
- Seleziona Aggiungi file esistenti al progetto
- selezionare la directory che si desidera importare
- Nella finestra pop-up assicuratevi di selezionare "Copia elementi nella cartella del gruppo di destinazione" e "creare riferimenti cartella per ogni cartelle aggiunte "
- " Aggiungi "
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 ;
}
questo: 'if (([Filemanager copyItemAtPath:! ResourceDBFolderPath topath: Errore documentDBFolderPath: & error ])) 'è illogico. Stai verificando se il metodo esiste, non se è riuscito. – CodaFi