iOS App 的檔案系統基於沙盒機制,不同目錄用途不同。正確存放資料有助於備份、效能與系統管理。
Documents 適合存放使用者資料與需要備份的檔案;Library/Preferences 存放設定;Library/Caches 存放可重建的快取;tmp 存放臨時檔案。
可使用 NSHomeDirectory 取得主目錄;使用 NSSearchPathForDirectoriesInDomains 取得 Documents 或 Caches;使用 NSTemporaryDirectory 取得 tmp 路徑。
若要取得 App bundle 內的圖片或檔案,可使用 NSBundle mainBundle 的 pathForResource:ofType:。檔案操作則可搭配 NSFileManager。