A file system handles the persistent storage of data files, apps, and the files associated with the operating system itself. Therefore, the file system is one of the fundamental resources used by all processes.
- A 文件系統(tǒng)處理數(shù)據(jù)文件求晶,應(yīng)用程序以及與操作系統(tǒng)本身相關(guān)的文件的持久存儲(chǔ)。 因此,文件系統(tǒng)是所有進(jìn)程使用的基本資源之一虱歪。
APFS is the default file system in macOS, iOS, watchOS, and tvOS. APFS replaces HFS+ as the default file system for iOS 10.3 and later, and macOS High Sierra and later. macOS additionally supports a variety of other formats, as described in Supported File Systems.
- APFS是macOS疼燥,iOS悲伶,watchOS和tvOS中的默認(rèn)文件系統(tǒng)咸包。 APFS將HFS +替換為iOS 10.3及更高版本以及macOS High Sierra及更高版本的默認(rèn)文件系統(tǒng)凛驮。 macOS還支持各種其他格式常侣,如支持的文件系統(tǒng)中所述蜡饵。
Regardless of the underlying format, all of the disks attached to the device—whether they are physically plugged in or are connected indirectly through the network—contribute space to create a single collection of files. Because the number of files can easily be many millions,the file system uses directories to create a hierarchical organization.
Although the basic directory structures are similar for iOS and macOS, there are differences in the way each system organizes apps and user data.
- 無(wú)論基礎(chǔ)格式如何,連接到設(shè)備的所有磁盤 - 無(wú)論是物理插入還是通過(guò)網(wǎng)絡(luò)間接連接 - 都會(huì)創(chuàng)建單個(gè)文件集合胳施。 由于文件數(shù)量很容易達(dá)到數(shù)百萬(wàn)溯祸,
因此文件系統(tǒng)使用目錄來(lái)創(chuàng)建分層組織
。 雖然iOS和macOS的基本目錄結(jié)構(gòu)相似舞肆,但每個(gè)系統(tǒng)組織應(yīng)用程序和用戶數(shù)據(jù)的方式各不相同焦辅。
Before you begin writing code that interacts with the file system, you should first understand a little about the organization of file system and the rules that apply to your code. Aside from the basic tenet that you cannot write files to directories for which you do not have appropriate security privileges, apps are also expected to be good citizens and put files in appropriate places. Precisely where you put files depends on the platform, but the overarching goal is to make sure that the user’s files remain easily discoverable and that the files your code uses internally are kept out of the user’s way.
- 在開(kāi)始編寫(xiě)與文件系統(tǒng)交互的代碼之前,您應(yīng)該先了解一下文件系統(tǒng)的組織以及適用于您的代碼的規(guī)則椿胯。 除了您無(wú)法將文件寫(xiě)入您沒(méi)有適當(dāng)安全權(quán)限的目錄的基本原則之外筷登,應(yīng)用程序也應(yīng)該是好公民并將文件放在適當(dāng)?shù)奈恢谩?準(zhǔn)確放置文件的位置取決于平臺(tái),但最重要的目標(biāo)是確保用戶的文件易于發(fā)現(xiàn)哩盲,并且代碼內(nèi)部使用的文件不受用戶限制前方。
About the iOS File System
- 關(guān)于iOS文件系統(tǒng)
The iOS file system is geared toward apps running on their own. To keep the system simple, users of iOS devices do not have direct access to the file system and apps are expected to follow this convention.
- iOS文件系統(tǒng)適用于自行運(yùn)行的應(yīng)用程序狈醉。 為了保持系統(tǒng)簡(jiǎn)單,iOS設(shè)備的用戶無(wú)法直接訪問(wèn)文件系統(tǒng)惠险,并且應(yīng)用程序應(yīng)遵循此約定苗傅。
iOS Standard Directories: Where Files Reside
- iOS標(biāo)準(zhǔn)目錄:文件駐留的位置
For security purposes, an iOS app’s interactions with the file system are limited to the directories inside the app’s sandbox directory. During installation of a new app, the installer creates a number of container directories for the app inside the sandbox directory. Each container directory has a specific role. The bundle container directory holds the app’s bundle, whereas the data container directory holds data for both the app and the user. The data container directory is further divided into a number of subdirectories that the app can use to sort and organize its data. The app may also request access to additional container directories—for example, the iCloud container—at runtime. - 出于安全考慮,iOS應(yīng)用程序與文件系統(tǒng)的交互僅限于應(yīng)用程序沙盒中的目錄班巩。 在安裝新應(yīng)用程序期間渣慕,安裝程序會(huì)在沙盒目錄中為應(yīng)用程序創(chuàng)建許多容器目錄。 每個(gè)容器目錄都有一個(gè)特定的角色趣竣。 包容器目錄包含應(yīng)用程序包摇庙,而數(shù)據(jù)容器目錄包含應(yīng)用程序和用戶的數(shù)據(jù)。 數(shù)據(jù)容器目錄進(jìn)一步劃分為許多子目錄遥缕,應(yīng)用程序可以使用這些子目錄對(duì)數(shù)據(jù)進(jìn)行排序和組織卫袒。 應(yīng)用程序還可以在運(yùn)行時(shí)請(qǐng)求訪問(wèn)其他容器目錄(例如,iCloud容器)单匣。
These container directories constitute the app’s primary view of the file system. Figure 1-1 shows a representation of the sandbox directory for an app.
- 這些容器目錄構(gòu)成了應(yīng)用程序的文件系統(tǒng)主視圖夕凝。 圖1-1顯示了應(yīng)用程序的沙箱目錄的表示。
在自己的沙箱目錄中運(yùn)行的iOS應(yīng)用程序
An app is generally prohibited from accessing or creating files outside its container directories. One exception to this rule is when an app uses public system interfaces to access things such as the user’s contacts or music. In those cases, the system frameworks use helper apps to handle any file-related operations needed to read from or modify the appropriate data stores.
- 通常禁止應(yīng)用程序訪問(wèn)或創(chuàng)建容器目錄之外的文件户秤。 此規(guī)則的一個(gè)例外是當(dāng)應(yīng)用程序使用公共系統(tǒng)接口訪問(wèn)用戶的聯(lián)系人或音樂(lè)等內(nèi)容時(shí)码秉。 在這些情況下,系統(tǒng)框架使用幫助應(yīng)用程序來(lái)處理讀取或修改適當(dāng)數(shù)據(jù)存儲(chǔ)所需的任何與文件相關(guān)的操作鸡号。
Table 1-1 lists some of the more important subdirectories inside the sandbox directory and describes their intended usage. This table also describes any additional access restrictions for each subdirectory and points out whether the directory’s contents are backed up by iTunes and iCloud.
- 表1-1列出了sandbox目錄中一些更重要的子目錄转砖,并描述了它們的預(yù)期用法。 此表還描述了每個(gè)子目錄的任何其他訪問(wèn)限制鲸伴,并指出目錄的內(nèi)容是否由iTunes和iCloud備份府蔗。
Table 1-1 Commonly used directories of an iOS app
- 常用的iOS應(yīng)用程序目錄
Directory1 :AppName.app
Description: This is the app’s bundle. This directory contains the app and all of its resources.
- 這是應(yīng)用程序的捆綁包。 該目錄包含應(yīng)用程序及其所有資源汞窗。
You cannot write to this directory. To prevent tampering, the bundle directory is signed at installation time. Writing to this directory changes the signature and prevents your app from launching. You can, however, gain read-only access to any resources stored in the apps bundle. For more information, see the Resource Programming Guide
- 您無(wú)法寫(xiě)入此目錄姓赤。 為防止篡改,bundle目錄在安裝時(shí)簽名仲吏。 寫(xiě)入此目錄會(huì)更改簽名并阻止您的應(yīng)用啟動(dòng)不铆。 但是,您可以獲得對(duì)應(yīng)用程序包中存儲(chǔ)的任何資源的只讀訪問(wèn)權(quán)限裹唆。 有關(guān)更多信息誓斥,請(qǐng)參閱“資源編程指南”
The contents of this directory are not backed up by iTunes or iCloud. However, iTunes does perform an initial sync of any apps purchased from the App Store.
- iTunes或iCloud不會(huì)備份此目錄的內(nèi)容。 但是许帐,iTunes確實(shí)執(zhí)行從App Store購(gòu)買的任何應(yīng)用程序的初始同步劳坑。
Directory2 : Documents/
Description: Use this directory to store user-generated content. The contents of this directory can be made available to the user through file sharing; therefore, his directory should only contain files that you may wish to expose to the user.
The contents of this directory are backed up by iTunes and iCloud.
- 使用此目錄存儲(chǔ)用戶生成的內(nèi)容。 該目錄的內(nèi)容可以通過(guò)文件共享提供給用戶; 因此舞吭,他的目錄應(yīng)該只包含您可能希望向用戶公開(kāi)的文件泡垃。
該目錄的內(nèi)容由iTunes和iCloud備份。
Directory3 : Documents/Inbox
Description: Use this directory to access files that your app was asked to open by outside entities. Specifically, the Mail program places email attachments associated with your app in this directory. Document interaction controllers may also place files in it.
- 使用此目錄可以訪問(wèn)外部實(shí)體要求您打開(kāi)應(yīng)用程序的文件羡鸥。 具體來(lái)說(shuō)蔑穴,Mail程序?qū)⑴c您的應(yīng)用程序關(guān)聯(lián)的電子郵件附件放在此目錄中。 文檔交互控制器也可以在其中放置文件惧浴。
Your app can read and delete files in this directory but cannot create new files or write to existing files. If the user tries to edit a file in this directory, your app must silently move it out of the directory before making any changes.
The contents of this directory are backed up by iTunes and iCloud.
- 您的應(yīng)用可以讀取和刪除此目錄中的文件存和,但無(wú)法創(chuàng)建新文件或?qū)懭氍F(xiàn)有文件。 如果用戶嘗試編輯此目錄中的文件衷旅,則在進(jìn)行任何更改之前捐腿,您的應(yīng)用必須以靜默方式將其移出目錄。
該目錄的內(nèi)容由iTunes和iCloud備份柿顶。
Directory4 : Library/
Description: This is the top-level directory for any files that are not user data files. You typically put files in one of several standard subdirectories. iOS apps commonly use the Application Support
and Caches
subdirectories; however, you can create custom subdirectories.
- 這是非用戶數(shù)據(jù)文件的任何文件的頂級(jí)目錄茄袖。 您通常將文件放在幾個(gè)標(biāo)準(zhǔn)子目錄之一中。 iOS應(yīng)用程序通常使用Application Support和Caches子目錄; 但是嘁锯,您可以創(chuàng)建自定義子目錄宪祥。
Use the Library
subdirectories for any files you don’t want exposed to the user. Your app should not use these directories for user data files.
- 對(duì)于您不希望向用戶公開(kāi)的任何文件,請(qǐng)使用Library子目錄家乘。 您的應(yīng)用不應(yīng)將這些目錄用于用戶數(shù)據(jù)文件蝗羊。
The contents of the Library
directory (with the exception of the Caches
subdirectory) are backed up by iTunes and iCloud.
- “Library”目錄的內(nèi)容(“Caches”子目錄除外)由iTunes和iCloud備份。
For additional information about the Library directory and its commonly used subdirectories, see The Library Directory Stores App-Specific Files.
- 有關(guān)Library目錄及其常用子目錄的其他信息仁锯,請(qǐng)參閱庫(kù)目錄存儲(chǔ)特定于應(yīng)用程序的文件耀找。
Directory5 : tmp/
Description:
Description: Use this directory to write temporary files that do not need to persist between launches of your app. Your app should remove files from this directory when they are no longer needed; however, the system may purge this directory when your app is not running.
- 使用此目錄可以編寫(xiě)在應(yīng)用程序啟動(dòng)之間不需要保留的臨時(shí)文件。 您的應(yīng)用程序應(yīng)在不再需要時(shí)刪除此目錄中的文件; 但是业崖,當(dāng)您的應(yīng)用未運(yùn)行時(shí)野芒,系統(tǒng)可能會(huì)清除此目錄。
The contents of this directory are not backed up by iTunes or iCloud.
- iTunes或iCloud不會(huì)備份此目錄的內(nèi)容腻要。
An iOS app may create additional directories in the Documents
, Library
, and tmp
directories. You might do this to better organize the files in those locations.
- iOS應(yīng)用程序可能會(huì)在Documents复罐,Library和tmp目錄中創(chuàng)建其他目錄。 您可以這樣做以更好地組織這些位置中的文件雄家。
For information about how to get references to the preceding directories from your iOS app, see Locating Items in the Standard Directories. For tips on where to put files, see Where You Should Put Your App’s Files.
- 有關(guān)如何從iOS應(yīng)用程序獲取對(duì)前面目錄的引用的信息效诅,請(qǐng)參閱在標(biāo)準(zhǔn)目錄中查找項(xiàng)目。 有關(guān)放置文件的位置的提示趟济,請(qǐng)參閱應(yīng)放置應(yīng)用程序文件的位置乱投。
Where You Should Put Your App’s Files
// 你應(yīng)該把你的應(yīng)用程序的文件放在哪里
To prevent the syncing and backup processes on iOS devices from taking a long time, be selective about where you place files. Apps that store large files can slow down the process of backing up to iTunes or iCloud. These apps can also consume a large amount of a user's available storage, which may encourage the user to delete the app or disable backup of that app's data to iCloud. With this in mind, you should store app data according to the following guidelines:
- 要防止iOS設(shè)備上的同步和備份過(guò)程花費(fèi)很長(zhǎng)時(shí)間,請(qǐng)選擇放置文件的位置顷编。 存儲(chǔ)大型文件的應(yīng)用程序可能會(huì)降低備份到iTunes或iCloud的速度戚炫。 這些應(yīng)用程序還可能消耗大量用戶的可用存儲(chǔ)空間,這可能會(huì)鼓勵(lì)用戶刪除應(yīng)用程序或禁用該應(yīng)用程序數(shù)據(jù)備份到iCloud媳纬。 考慮到這一點(diǎn)双肤,您應(yīng)該根據(jù)以下準(zhǔn)則存儲(chǔ)應(yīng)用數(shù)據(jù):
-
Put user data in
Documents/
. User data generally includes any files you might want to expose to the user—anything you might want the user to create, import, delete or edit. For a drawing app, user data includes any graphic files the user might create. For a text editor, it includes the text files. Video and audio apps may even include files that the user has downloaded to watch or listen to later.- 將用戶數(shù)據(jù)放在Documents /中施掏。 用戶數(shù)據(jù)通常包括您可能希望向用戶公開(kāi)的任何文件 - 您可能希望用戶創(chuàng)建,導(dǎo)入茅糜,刪除或編輯的任何文件七芭。 對(duì)于繪圖應(yīng)用程序,用戶數(shù)據(jù)包括用戶可能創(chuàng)建的任何圖形文件蔑赘。 對(duì)于文本編輯器狸驳,它包含文本文件。 視頻和音頻應(yīng)用程序甚至可能包含用戶下載后觀看或收聽(tīng)的文件缩赛。
-
Put app-created support files in the
Library/Application support/
directory. In general, this directory includes files that the app uses to run but that should remain hidden from the user. This directory can also include data files, configuration files, templates and modified versions of resources loaded from the app bundle.- 將應(yīng)用程序創(chuàng)建的支持文件放在庫(kù)/應(yīng)用程序支持/目錄中耙箍。 通常,此目錄包含應(yīng)用程序用于運(yùn)行但應(yīng)保持對(duì)用戶隱藏的文件酥馍。 此目錄還可以包括從應(yīng)用程序包加載的數(shù)據(jù)文件辩昆,配置文件,模板和已修改的資源版本物喷。
-
Remember that files in
Documents/
andApplication Support/
are backed up by default. You can exclude files from the backup by calling-[NSURL setResourceValue:forKey:error:]
using theNSURLIsExcludedFromBackupKey
key. Any file that can be re-created or downloaded must be excluded from the backup. This is particularly important for large media files. If your application downloads video or audio files, make sure they are not included in the backup.- 請(qǐng)記住卤材,默認(rèn)情況下會(huì)備份Documents /和Application Support /中的文件。 您可以使用NSURLIsExcludedFromBackupKey鍵調(diào)用[NSURL setResourceValue:forKey:error:]從備份中排除文件峦失。 必須從備份中排除任何可以重新創(chuàng)建或下載的文件扇丛。 這對(duì)于大型媒體文件尤為重要。 如果您的應(yīng)用程序下載視頻或音頻文件尉辑,請(qǐng)確保它們不包含在備份中帆精。
-
Put temporary data in the
tmp/
directory. Temporary data comprises any data that you do not need to persist for an extended period of time. Remember to delete those files when you are done with them so that they do not continue to consume space on the user’s device. The system will periodically purge these files when your app is not running; therefore, you cannot rely on these files persisting after your app terminates.- 將臨時(shí)數(shù)據(jù)放在tmp /目錄中。 臨時(shí)數(shù)據(jù)包括您不需要長(zhǎng)時(shí)間保留的任何數(shù)據(jù)隧魄。 請(qǐng)記住在完成這些文件后刪除這些文件卓练,以便它們不會(huì)繼續(xù)消耗用戶設(shè)備上的空間。 當(dāng)您的應(yīng)用未運(yùn)行時(shí)购啄,系統(tǒng)會(huì)定期清除這些文件; 因此襟企,您的應(yīng)用程序終止后,您不能依賴這些文件狮含。
-
Put data cache files in the
Library/Caches/
directory. Cache data can be used for any data that needs to persist longer than temporary data, but not as long as a support file. Generally speaking, the application does not require cache data to operate properly, but it can use cache data to improve performance. Examples of cache data include (but are not limited to) database cache files and transient, downloadable content. Note that the system may delete theCaches/
directory to free up disk space, so your app must be able to re-create or download these files as needed.- 將數(shù)據(jù)緩存文件放在Library / Caches /目錄中顽悼。 緩存數(shù)據(jù)可用于需要比臨時(shí)數(shù)據(jù)持續(xù)更長(zhǎng)時(shí)間的任何數(shù)據(jù),但不能與支持文件一樣長(zhǎng)几迄。 一般來(lái)說(shuō)蔚龙,應(yīng)用程序不需要緩存數(shù)據(jù)正常運(yùn)行,但它可以使用緩存數(shù)據(jù)來(lái)提高性能映胁。 高速緩存數(shù)據(jù)的示例包括(但不限于)數(shù)據(jù)庫(kù)高速緩存文件和瞬態(tài)可下載內(nèi)容木羹。 請(qǐng)注意,系統(tǒng)可能會(huì)刪除Caches /目錄以釋放磁盤空間解孙,因此您的應(yīng)用必須能夠根據(jù)需要重新創(chuàng)建或下載這些文件坑填。
How the System Identifies the Type of Content in a File
- 系統(tǒng)如何識(shí)別文件中的內(nèi)容類型
There are two primary techniques for identifying the type of content in a file:
- 有兩種主要技術(shù)可用于識(shí)別文件中的內(nèi)容類型:
Uniform Type Identifiers (UTIs): 統(tǒng)一類型標(biāo)識(shí)符(UTI)
Filename extensions: 文件擴(kuò)展名
A uniform type identifier is a string that uniquely identifies a class of entities considered to have a “type.” UTIs provide consistent identifiers for data that all apps and services can recognize and rely upon. They are also more flexible than most other techniques because you can use them to represent any type of data, not just files and directories. Examples of UTIs include:
- 統(tǒng)一類型標(biāo)識(shí)符是唯一標(biāo)識(shí)被認(rèn)為具有“類型”的實(shí)體類的字符串.UTI為所有應(yīng)用和服務(wù)可識(shí)別和依賴的數(shù)據(jù)提供一致的標(biāo)識(shí)符抛人。 它們也比大多數(shù)其他技術(shù)更靈活,因?yàn)槟梢允褂盟鼈儊?lái)表示任何類型的數(shù)據(jù)脐瑰,而不僅僅是文件和目錄函匕。 UTI的例子包括:
public.text—A public type that identifies text data.
- 標(biāo)識(shí)文本數(shù)據(jù)的公共類型。
public.jpeg—A public type that identifies JPEG image data.
- 標(biāo)識(shí)JPEG圖像數(shù)據(jù)的公共類型蚪黑。
com.apple.bundle—An Apple type that identifies a bundle directory.
- 標(biāo)識(shí)捆綁目錄的Apple類型。
com.apple.application-bundle—An Apple type that identifies a bundled app.
- 標(biāo)識(shí)捆綁應(yīng)用的Apple類型中剩。
Whenever a UTI-based interface is available for specifying file types, you should prefer that interface over any others. Many macOS interfaces allow you to specify UTIs corresponding to the files or directories you want to work with. For example, in the Open panel, you can use UTIs as file filters and limit the types of files the user selects to ones your app can handle. Several AppKit classes, including NSDocument, NSPasteboard, and NSImage, support UTIs. In iOS, UTIs are used to specify pasteboard types only.
- 每當(dāng)基于UTI的接口可用于指定文件類型時(shí)忌穿,您應(yīng)該優(yōu)先于任何其他接口。 許多macOS接口允許您指定與要使用的文件或目錄相對(duì)應(yīng)的UTI结啼。 例如掠剑,在“打開(kāi)”面板中,您可以將UTI用作文件篩選器郊愧,并將用戶選擇的文件類型限制為應(yīng)用程序可以處理的文件類型朴译。 幾個(gè)AppKit類,包括NSDocument属铁,NSPasteboard和NSImage眠寿,都支持UTI。 在iOS中焦蘑,UTI僅用于指定粘貼板類型盯拱。
One way the system determines the UTI for a given file is by looking at its filename extension. A filename extension is a string of characters appended to the end of a file and separated from the main filename with a period. Each unique string of characters identifies a file of a specific type. For example, the .strings extension identifies a resource file with localizable string data while the .png extension identifies a file with image data in the portable network graphics format.
- 系統(tǒng)確定給定文件的UTI的一種方法是查看其文件擴(kuò)展名。 文件擴(kuò)展名是附加到文件末尾的字符串例嘱,并使用句點(diǎn)與主文件名分隔狡逢。 每個(gè)唯一的字符串標(biāo)識(shí)特定類型的文件。 例如拼卵,.strings擴(kuò)展名標(biāo)識(shí)具有可本地化字符串?dāng)?shù)據(jù)的資源文件奢浑,而.png擴(kuò)展名標(biāo)識(shí)具有便攜式網(wǎng)絡(luò)圖形格式的圖像數(shù)據(jù)的文件。
Note: Because period characters are valid characters in macOS and iOS filenames, only the characters after the last period in a filename are considered part of the filename extension. Everything to the left of the last period is considered part of the filename itself.
- 由于句點(diǎn)字符是macOS和iOS文件名中的有效字符腋腮,因此只有文件名中最后一個(gè)句點(diǎn)之后的字符才會(huì)被視為文件擴(kuò)展名的一部分雀彼。 最后一個(gè)周期左邊的所有內(nèi)容都被視為文件名本身的一部分。
If your app defines custom file formats, you should register those formats and any associated filename extensions in your app’s Info.plist
file. The CFBundleDocumentTypes
key specifies the file formats that your app recognizes and is able to open. Entries for any custom file formats should include both a filename extension and UTI corresponding to the file contents. The system uses that information to direct files with the appropriate type to your app.
如果您的應(yīng)用定義了自定義文件格式低葫,則應(yīng)在應(yīng)用的Info.plist文件中注冊(cè)這些格式和任何相關(guān)的文件擴(kuò)展名详羡。 CFBundleDocumentTypes鍵指定應(yīng)用識(shí)別并能夠打開(kāi)的文件格式。 任何自定義文件格式的條目都應(yīng)包括文件擴(kuò)展名和與文件內(nèi)容對(duì)應(yīng)的UTI嘿悬。 系統(tǒng)使用該信息將具有適當(dāng)類型的文件定向到您的應(yīng)用程序实柠。
For more information about UTIs and how you use them, see Uniform Type Identifiers Overview. For more information about the CFBundleDocumentTypes
key, see Information Property List Key Reference.
Files Can Be Encrypted On Disk
Both macOS and iOS provide support for encrypting files on disk:
-
iOS. An iOS app can designate files that it wants to be encrypted on disk. When the user unlocks a device containing encrypted files, the system creates a decryption key that allows the app to access its encrypted files. When the user locks the device, though, the decryption key is destroyed to prevent unauthorized access to the files.
- iOS應(yīng)用程序可以指定要在磁盤上加密的文件。 當(dāng)用戶解鎖包含加密文件的設(shè)備時(shí)善涨,系統(tǒng)會(huì)創(chuàng)建一個(gè)解密密鑰窒盐,允許該應(yīng)用訪問(wèn)其加密文件草则。 但是,當(dāng)用戶鎖定設(shè)備時(shí)蟹漓,破解解密密鑰以防止對(duì)文件的未授權(quán)訪問(wèn)炕横。
In iOS, apps that take advantage of disk-based encryption need to be discontinue the use of encrypted files when the user locks the device. Because locking the device destroys the decryption keys, access to encrypted files is limited to when the device is unlocked. If your iOS app can run in the background while the device is locked, it must do so without access to any of its encrypted files. Because encrypted disks in macOS are always accessible while the computer is running, macOS apps do not need to do anything special to handle disk-level encryption.
- 在iOS中,利用基于磁盤的加密的應(yīng)用程序需要在用戶鎖定設(shè)備時(shí)停止使用加密文件葡粒。 由于鎖定設(shè)備會(huì)破壞解密密鑰份殿,因此對(duì)加密文件的訪問(wèn)僅限于設(shè)備解鎖時(shí)。 如果您的iOS應(yīng)用程序可以在設(shè)備鎖定時(shí)在后臺(tái)運(yùn)行嗽交,則必須在不訪問(wèn)任何加密文件的情況下執(zhí)行此操作卿嘲。 由于macOS中的加密磁盤在計(jì)算機(jī)運(yùn)行時(shí)始終可以訪問(wèn),因此macOS應(yīng)用程序無(wú)需執(zhí)行任何特殊操作即可處理磁盤級(jí)加密夫壁。
For more information about working with encrypted files in iOS, see App Programming Guide for iOS.
Files, Concurrency, and Thread Safety
Because file-related operations involve interacting with the hard disk and are therefore slow compared to most other operations, most of the file-related interfaces in iOS and macOS are designed with concurrency in mind. Several technologies incorporate asynchronous operation into their design and most others can execute safely from a dispatch queue or secondary thread. Table 1-4 lists some of the key technologies discussed in this document and whether they are safe to use from specific threads or any thread. For specific information about the capabilities of any interface, see the reference documentation for that interface.
- 由于與文件相關(guān)的操作涉及與硬盤交互拾枣,因此與大多數(shù)其他操作相比較慢,因此iOS和macOS中的大多數(shù)與文件相關(guān)的接口都考慮了并發(fā)性盒让。 有幾種技術(shù)將異步操作合并到其設(shè)計(jì)中梅肤,而大多數(shù)其他技術(shù)可以從調(diào)度隊(duì)列或輔助線程安全地執(zhí)行。 表1-4列出了本文檔中討論的一些關(guān)鍵技術(shù)邑茄,以及它們是否可以安全地從特定線程或任何線程使用姨蝴。 有關(guān)任何接口功能的特定信息,請(qǐng)參閱該接口的參考文檔肺缕。
-
NSFileManager
For most tasks, it is safe to use the default NSFileManager object simultaneously from multiple background threads. The only exception to this rule is tasks that interact with the file manager’s delegate. When using a file manager object with a delegate, it is recommended that you create a unique instance of the NSFileManager class and use your delegate with that instance. You should then use your unique instance from one thread at a time.
- 對(duì)于大多數(shù)任務(wù)似扔,可以安全地從多個(gè)后臺(tái)線程同時(shí)使用默認(rèn)的NSFileManager對(duì)象。 此規(guī)則的唯一例外是與文件管理器委托交互的任務(wù)搓谆。 將文件管理器對(duì)象與委托一起使用時(shí)炒辉,建議您創(chuàng)建NSFileManager類的唯一實(shí)例,并將該委托與該實(shí)例一起使用泉手。 然后黔寇,您應(yīng)該一次使用一個(gè)線程中的唯一實(shí)例。
- Grand Central Dispatch
GCD itself is safe to use from any thread. However, you are still responsible for writing your blocks in a way that is thread safe.
- GCD本身可以安全地從任何線程使用斩萌。 但是缝裤,您仍然負(fù)責(zé)以線程安全的方式編寫(xiě)塊。
-
NSFileHandle
,
NSData, Cocoa streams
Most of the Foundation objects you use to read and write file data can be used from any single thread but should not be used from multiple threads simultaneously.
- 您用于讀取和寫(xiě)入文件數(shù)據(jù)的大多數(shù)Foundation對(duì)象都可以在任何單個(gè)線程中使用颊郎,但不應(yīng)同時(shí)從多個(gè)線程使用憋飞。
- Open and Save panels
Because they are part of your user interface, you should always present and manipulate the Open and Save panels from your app’s main thread.
- 因?yàn)樗鼈兪怯脩艚缑娴囊徊糠郑詰?yīng)始終在應(yīng)用程序的主線程中顯示和操作“打開(kāi)”和“保存”面板姆吭。
- POSIX routines
The POSIX routines for manipulating files are generally designed to operate safely from any thread. For details, see the corresponding man pages.
- 用于操作文件的POSIX例程通常設(shè)計(jì)為從任何線程安全地操作榛做。 有關(guān)詳細(xì)信息,請(qǐng)參見(jiàn)相應(yīng)的手冊(cè)頁(yè)。
-
NSURL
and
NSString
The immutable objects you use to specify paths are safe to use from any thread. Because they are immutable, you can also refer to them from multiple threads simultaneously. Of course, the mutable versions of these objects should be used from only one thread at a time.
- 用于指定路徑的不可變對(duì)象可以安全地從任何線程使用检眯。 因?yàn)樗鼈兪遣豢勺兊睦謇蓿阅部梢酝瑫r(shí)從多個(gè)線程中引用它們。 當(dāng)然锰瘸,這些對(duì)象的可變版本一次只能在一個(gè)線程中使用刽严。
-
NSEnumerator and its subclasses
Enumerator objects are safe to use from any single thread but should not be used from multiple threads simultaneously.
- 枚舉器對(duì)象可以安全地從任何單個(gè)線程使用,但不應(yīng)同時(shí)從多個(gè)線程使用避凝。
Even if you use an thread-safe interface for manipulating a file, problems can still arise when multiple threads or multiple processes attempt to act on the same file. Although there are safeguards to prevent multiple clients from modifying a file at the same time, those safeguards do not always guarantee exclusive access to the file at all times. (Nor should you attempt to prevent other processes from accessing shared files.) To make sure your code knows about changes made to shared files, use file coordinators to manage access to those files. For more information about file coordinators, see The Role of File Coordinators and Presenters
- 即使您使用線程安全接口來(lái)操作文件舞萄,當(dāng)多個(gè)線程或多個(gè)進(jìn)程嘗試對(duì)同一文件執(zhí)行操作時(shí),仍會(huì)出現(xiàn)問(wèn)題管削。 盡管存在防止多個(gè)客戶端同時(shí)修改文件的安全措施鹏氧,但這些安全措施并不總能保證對(duì)文件的獨(dú)占訪問(wèn)。 (您也不應(yīng)該嘗試阻止其他進(jìn)程訪問(wèn)共享文件佩谣。)要確保您的代碼知道對(duì)共享文件所做的更改,請(qǐng)使用文件協(xié)調(diào)器來(lái)管理對(duì)這些文件的訪問(wèn)实蓬。 有關(guān)文件協(xié)調(diào)器的更多信息茸俭,請(qǐng)參閱文件協(xié)調(diào)器和演示者的角色