2011-11-06 37 views
28

Ok,Làm thế nào để tạo một thư mục iOS?

Vì vậy, tôi có ứng dụng Cydia mà tôi cần cập nhật. Tôi nhận thức được với các ứng dụng Cydia rằng họ không có thư mục Documents, vì vậy bạn phải tạo một tài liệu. Và đây là cách tôi tạo nó trước đây trong iOS 4 (không hoạt động trên 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]; 

} 

Tôi cũng bao gồm mã sao chép tệp cơ sở dữ liệu vào thư mục đó. Điều đó không hoạt động (ngay cả khi tôi tạo thư mục theo cách thủ công qua SSH).

Vui lòng trợ giúp! Cảm ơn.

+0

Câu hỏi của bạn là gì? –

+0

Điều gì đang xảy ra, hoặc đoạn mã nào khác mà tôi có thể sử dụng để tạo thư mục? – iosfreak

+0

Đúng, chuyện gì đang xảy ra? Bạn đã không nói với chúng tôi. Không có thông báo lỗi, không có mô tả về lỗi. –

Trả lời

59

Dưới đây là phương pháp tôi đã thực hiện để tạo ra các thư mục

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

Đây là lỗi tôi nhận được ... 'Thao tác không thể hoàn thành. Hoạt động không được phép' – iosfreak

+2

Đây có thể là những gì bạn đang tìm kiếm http://stackoverflow.com/questions/4650488/is-an-iphone-apps-document-directory-var-mobile-documents-or-var-mobile-libra – syclonefx

3

Kiểm tra NSFileManager's class reference. Để tạo thư mục bạn cần createDirectoryAtPath:withIntermediateDirectories:attributes:error:

+0

Tôi đã thử và không có thư mục nào được tạo ... Tôi nghĩ rằng đó chỉ dành cho OSx chứ không phải iOS? – iosfreak

+0

Bạn đã sử dụng 'YES' làm đối số của' withIntermediateDirectories: '? – Jef

+0

'NSFileManager * NSFm = [NSFileManager defaultManager]; [NSFm createDirectoryAtPath: @ "/ var/mobile/Library/APPNAME" withIntermediateDirectories: YES thuộc tính: nil error: nil]; [NSFm createDirectoryAtPath: @ "/ var/mobile/Library/APPNAME/Documents" withIntermediateDirectories: YES thuộc tính: nil error: nil]; 'là mã chính xác tôi đã sử dụng ... – iosfreak

4

Hãy thử sử dụng 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

Dường như Im hơi chậm khi trả lời từ iPad của tôi ..., câu trả lời khác dường như khá giống với điều này. – chown

0

Trong Swift, trả về true nếu tồn tại hoặc tạo ra.

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 
} 
Các vấn đề liên quan