2011-11-06 15 views
28

Ok,Come creare una directory iOS?

Quindi ho un'app Cydia che devo aggiornare. Con le app Cydia sono consapevole che non hanno una cartella Documenti, quindi devi crearne una. Ed ecco come ho fatto prima in iOS 4 (che non funziona su iOS 5):

mkdir("/var/mobile/Library/APPNAME", 0755); 
mkdir("/var/mobile/Library/APPNAME/Documents", 0755); 

NSString *foofile = @"/var/mobile/Library/APPNAME/Documents/database.db"; 
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile]; 

if (fileExists == TRUE) { 
    NSLog(@"already exists"); 
} else { 
    NSLog(@"doesn't exists"); 
    NSFileManager *fileManager = [[NSFileManager defaultManager]autorelease]; 
    NSError *error; 
    NSString *documentDBFolderPath = @"/var/mobile/Library/APPNAME/Documents/database.db"; 

    NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.db"]; 
    [fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error]; 

} 

ho incluso anche il codice che copia il file di database in quella cartella, anche. Non funziona (anche quando creo la cartella manualmente tramite SSH).

Si prega di aiuto! Grazie.

+0

Qual è la tua domanda? –

+0

Cosa non va, o quale altra parte di codice posso usare per creare le directory? – iosfreak

+0

Giusto, che cosa sta andando male? Non ci hai detto. Nessun messaggio di errore, nessuna descrizione dell'errore. –

risposta

59

Ecco il metodo che ho fatto per creare le directory

-(void)createDirectory:(NSString *)directoryName atFilePath:(NSString *)filePath 
{ 
    NSString *filePathAndDirectory = [filePath stringByAppendingPathComponent:directoryName]; 
    NSError *error; 

    if (![[NSFileManager defaultManager] createDirectoryAtPath:filePathAndDirectory 
            withIntermediateDirectories:NO 
                attributes:nil 
                 error:&error]) 
    { 
     NSLog(@"Create directory error: %@", error); 
    } 
} 
+0

Questo è l'errore che ottengo ... 'L'operazione non può essere completata. Il funzionamento non permitted' – iosfreak

+2

Questo potrebbe essere quello che cercate http://stackoverflow.com/questions/4650488/is-an-iphone-apps-document-directory-var-mobile-documents-or-var-mobile-libra – syclonefx

3

Controllare NSFileManager's class reference. Per creare cartelle è necessario createDirectoryAtPath:withIntermediateDirectories:attributes:error:

+0

ho provato e nessun cartella è stata creata ... I pensi che sia solo per OSx, non per iOS? – iosfreak

+0

Hai usato 'YES' come argomento di' withIntermediateDirectories: '? – Jef

+0

'NSFileManager * NSFm = [NSFileManager defaultManager]; [NSFm createDirectoryAtPath: @ "/ var/mobile/Library/APPNAME" conIntermediateDirectories: attributi YES: errore nil: nil]; [NSFm createDirectoryAtPath: @ "/ var/mobile/Library/APPNAME/Documents" withIntermediateDirectories: SI attributi: errore pari a zero: nil]; 'era il codice esatto che ho usato ... – iosfreak

4

Provare a utilizzare createDirectoryAtURL:withIntermediateDirectories:attributes:error:.

NSFileManager Class Reference:

createDirectoryAtURL:withIntermediateDirectories:attributes:error:

Creates a directory with given attributes at the specified path.

Parameters

url - A file URL that specifies the directory to create. If you want to specify a relative path, you must set the current working directory before creating the corresponding NSURL object. This parameter must not be nil.

createIntermediates - If YES , this method creates any non-existent parent directories as part of creating the directory in url. If NO , this method fails if any of the intermediate parent directories does not exist. This method also fails if any of the intermediate path elements corresponds to a file and not a directory.

attributes - The file attributes for the new directory and any newly created intermediate directories. You can set the owner and group numbers, file permissions, and modification date. If you specify nil for this parameter or omit a particular value, one or more default values are used as described in the discussion. For a list of keys you can include in this dictionary, see “Constants” (page 54) section lists the global constants used as keys in the attributes dictionary. Some of the keys, such as NSFileHFSCreatorCode and NSFileHFSTypeCode, do not apply to directories.

error - On input, a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil for this parameter if you do not want the error information.

Return Value
YES if the directory was created or already exists or NO if an error occurred.

+0

Sembra un po 'lento quando rispondo dal mio iPad ..., l'altra risposta sembra essere praticamente la stessa di questa. – chown

0

In Swift, restituisce vero se esiste o creati.

func ensureDirectoryExists(path:String) -> Bool { 

    if !NSFileManager.defaultManager().fileExistsAtPath(path) { 
     do { 
      try NSFileManager.defaultManager().createDirectoryAtPath(path, withIntermediateDirectories: true, attributes: nil) 
     } catch { 
      print(error) 
      return false 
     } 
    } 
    return true 
} 
Problemi correlati