2011-11-06 23 views
28

好吧,如何製作一個目錄iOS?

所以我有一個Cydia應用程序,我需要更新。我知道與Cydia應用程序,他們沒有一個文件夾,所以你必須做一個。這裏是我如何做它之前在iOS 4的(這不會對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]; 

} 

我也包括代碼,數據庫文件複製到該文件夾​​了。這不起作用(即使我通過SSH手動創建文件夾)。

請幫忙!謝謝。

+0

什麼是你的問題? –

+0

發生了什麼問題,或者我可以使用哪些其他代碼創建目錄? – iosfreak

+0

對,出什麼事了?你沒有告訴我們。沒有錯誤信息,沒有描述失敗。 –

回答

59

這裏是我做了創建目錄

-(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

這是我得到的錯誤...'操作無法完成。操作不permitted' – iosfreak

+2

這可能是你想找的http://stackoverflow.com/questions/4650488/is-an-iphone-apps-document-directory-var-mobile-documents-or-var-mobile-libra – syclonefx

3

檢查NSFileManager's class reference。要創建文件夾,你需要使用createDirectoryAtURL:withIntermediateDirectories:attributes:error:createDirectoryAtPath:withIntermediateDirectories:attributes:error:

+0

我試過了,並沒有創建文件夾...我認爲這隻適用於OSx,不適用於iOS? – iosfreak

+0

你用'YES'作爲'withIntermediateDirectories:'的參數嗎? – Jef

+0

'NSFileManager * NSFm = [NSFileManager defaultManager]; [NSFm createDirectoryAtPath:@「/ var/mobile/Library/APPNAME」withIntermediateDirectories:YES attributes:nil error:nil]; [NSFm createDirectoryAtPath:@ 「的/ var /移動/庫/ APPNAME /文檔」 withIntermediateDirectories:YES屬性:無誤差:無];'是我使用的確切的代碼... – iosfreak

4

嘗試。

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

似乎從我的iPad回答有點慢...,另一個答案似乎與此相差無幾。 – chown

0

在斯威夫特的方法,如果存在或創建返回true。

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 
} 
相關問題