IOS擷取各種檔案目錄路徑的方法_IOS

來源:互聯網
上載者:User

iphone沙箱模型有四個檔案夾,分別是什麼,永久資料存放區一般放在什麼位置,得到模擬器的路徑的簡單方式是什麼.

documents,tmp,app,Library。

(NSHomeDirectory()),

手動儲存的檔案在documents檔案裡

Nsuserdefaults儲存的檔案在tmp檔案夾裡

1、Documents 目錄:您應該將所有de應用程式資料檔案寫入到這個目錄下。這個目錄用於儲存使用者資料或其它應該定期備份的資訊。

2、AppName.app 目錄:這是應用程式的程式包目錄,包含應用程式的本身。由於應用程式必須經過簽名,所以您在運行時不能對這個目錄中的內容進行修改,否則可能會使應用程式無法啟動。

3、Library 目錄:這個目錄下有兩個子目錄:Caches 和 Preferences
Preferences 目錄:包含應用程式的喜好設定檔案。您不應該直接建立喜好設定檔案,而是應該使用NSUserDefaults類來取得和設定應用程式的偏好.
Caches 目錄:用於存放應用程式專用的支援檔案,儲存應用程式再次啟動過程中需要的資訊。

4、tmp 目錄:這個目錄用於存放臨時檔案,儲存應用程式再次啟動過程中不需要的資訊。

擷取這些目錄路徑的方法:

第一種方式:擷取家目錄路徑的函數:

NSString *homeDir = NSHomeDirectory();


第二種方式:擷取Documents目錄路徑的方法:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *docDir = [paths objectAtIndex:0];

第三種方式:擷取Caches目錄路徑的方法:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);NSString *cachesDir = [paths objectAtIndex:0];

第四種方式:擷取tmp目錄路徑的方法:

NSString *tmpDir = NSTemporaryDirectory();

第五種方式:擷取應用程式程式包中資源檔路徑的方法:
例如擷取程式包中一個圖片資源(apple.png)路徑的方法:

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@”apple” ofType:@”png”];UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];

代碼中的mainBundle類方法用於返回一個代表應用程式套件組合的對象。

iphone沙箱(sandbox)中的幾個目錄擷取方式:

// 擷取沙箱主目錄路徑 

NSString *homeDir = NSHomeDirectory(); 

// 擷取Documents目錄路徑 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docDir = [paths objectAtIndex:0]; 

// 擷取Caches目錄路徑 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachesDir = [paths objectAtIndex:0]; 

// 擷取tmp目錄路徑 

NSString *tmpDir = NSTemporaryDirectory(); 

// 擷取當前程式包中一個圖片資源(apple.png)路徑 

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"apple" ofType:@"png"]; UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath]; 

例子:

NSFileManager* fm=[NSFileManager defaultManager];if(![fm fileExistsAtPath:[self dataFilePath]]){

//下面是對該檔案進行制定路徑的儲存

[fm createDirectoryAtPath:[self dataFilePath] withIntermediateDirectories:YES attributes:nil error:nil];

//取得一個目錄下得所有檔案名稱

NSArray *files = [fm subpathsAtPath: [self dataFilePath] ];

//讀取某個檔案

NSData *data = [fm contentsAtPath:[self dataFilePath]];

//或者

NSData *data = [NSData dataWithContentOfPath:[self dataFilePath]];}

ios擷取檔案路徑的方法有多種,下面介紹一種IOS中擷取檔案路徑比較簡單方法。

網上的DOCUMNET和“教程”真讓人越看越糊塗,還是自己記下吧。

首先把檔案(比如本例中的testFile.txt檔案)放置在resources分組下,然後代碼這樣寫:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"testFile" ofType:@"txt"];NSLog(@"data path: %@", filePath); 

輸出的日誌中你可以看到testFile.txt的路徑已經獲得。

再舉一個例子:串連SQLITE資料庫

NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"myData" ofType:@"sqlite"];if (sqlite3_open([dataPath UTF8String], &db) != SQLITE_OK){  sqlite3_close(db);  NSLog(@"資料庫開啟失敗");}else{  NSLog(@"資料庫成功開啟");}

以上內容是IOS擷取各種檔案目錄路徑的方法,希望對大家有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.