preface

Hi Coder, I’m CoderStar!

This article is the second in the iOS persistence series. For the first article, see UserDefaults and Its Use Management.

The driving license is about to expire, so I went out to change a license this weekend and spent some time. I had less time to sort out the article, so I chose a relatively small knowledge point to sort out and share with you. By the way, if you have any questions about the process of renewing your certificate in Beijing, you can also talk to me privately. After all, you have gone through the process and have some experience relatively.

Overall directory structure

Take a look at the overall directory structure. Note that this directory is not a sandbox directory for a single APP, but a directory structure for all apps in the system as a whole.

Real machine environment, the directory path for/private/var/mobile/Containers, if in the simulator, the directory path and the actual file path for Mac, take a certain path under the simulator for example: / Users/coderstar/Library/Developer/CoreSimulator/Devices / 1328 ff08 – efe – B936 D6CB – 4-5 e1cb5d03d75 / data/Containers.

  • Bundle:
    • Application
      • Folder named by the MD5 identifier of app
        • MyApp.app
        • BundleMetadata.plist
  • Data
    • Application
      • Folder named by the MD5 identifier of app
        • Documents
        • Library
          • Application Support
          • Caches
          • Cookies
          • Preferences
          • Saved Application State
          • SplashBoard
          • WebKit
        • SystemData
        • tmp
    • InternalDaemon
    • PluginKitPlugin
    • System
  • Shared
    • AppGroup:
      • Folder named by the MD5 identifier of app
        • Library
          • Caches
          • Preferences
    • SystemGroup
  • Staging
  • Temp

The directories listed above are not necessarily complete, and some of them are automatically created when the related files are first generated.

From the directory structure above, we can basically get the sandbox structure of APP, as shown below:

The XSimulatorMngr tool makes it easier to view files in the Sandbox under the simulator, and more tools can see the Mac Efficiency software.

Key Directory Interpretation

Although there are many subdirectories under the above directory structure, a large part of them are used by the system. Let’s take a look at some of the key directories.

Bundle/Application

Contains all resource files and executables, which are digitally signed before being uploaded and cannot be modified after being uploaded.

Documents

The contents of this directory can be made available to users through file sharing, so the directory should contain only files that you might want to expose to users. Use this directory to store user-generated content, such as user-created files or downloaded audio and video data files. ITunes and iCloud will back up this directory.

After iOS11, a file APP has been added to centrally manage files created in iOS apps and files saved in various cloud disk services. Set Application Supports iTunes File Sharing and Supports Opening Documents in Place to YES in iOS project info.plist (the default is YES NO), you can expose the files in the sandbox Documents path of the application to the files APP.

Library/Application Support

This directory contains files that the application uses to run but should be hidden from the user, such as new levels of the game. ITunes and iCloud will back up this directory.

Library/Caches

Stores persistent data generated during application running. Generally, it stores non-important data with large volume that does not need to be backed up, such as the cache of audio, video, and pictures requested by network. In iOS 5.0 and later, Caches when system disk space is very low, the system may delete the directory in rare cases (this does not happen while the APP is running), so try to ensure that files in this path can be recreated when the APP is re-run. ITunes and iCloud do not back up this directory.

Library/Preference

Save all of your app’s preferences. If you read the last article, you’ll remember that this is where the plist files generated by UserDefaults are stored. ITunes and iCloud will back up this directory.

Library/SplashBoard

The cache file format of the startup screen is KTX, which is essentially a picture. If the startup screen does not take effect, it can be solved from the perspective of deleting relevant cache files in this path.

Library/WebKit

Store WKWebView related data, such as IndexDB, LocalStorage, WebSQL, etc.

tmp

Save some temporary data generated while the application is running. If an application exits, the system space is insufficient, or the mobile phone restarts, the system automatically clears the data in the directory. ITunes and iCloud do not back up this directory.

AppGroup

Host program and extender data sharing area.

Subdirectory Library/Preferences, which does not exist by default and is created when group UserDefaults are created. The plist name corresponding to the UserDefaults is the group name.

Mode of operation

Obtain path address

/// Sandbox home directory
let path = NSHomeDirectory(a)/ / / Documtents directory
let documtentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!

/ / / the Library catalog
let libraryPath = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true).first!

/// Application Support directory
let applicationSupportPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).first!

 / / / Caches directory
let cachesPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first!

/ / / TMP directory
let tmpPath = NSTemporaryDirectory(a)/ * * -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - the above form to obtain path return value to a String, Returns the value of the URL form according to actual situation to choose the appropriate way to -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * /

let dataURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("sqliteName").appendingPathExtension("sqlite")



/// AppGroup directory path
let appGroupIdentifier = "group.com.star.LTXiOSUtils.extension"
let groupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appgroupIdentifier)
Copy the code

Please note NSSearchPathForDirectoriesInDomains (preferencePanesDirectory, userDomainMask, true). The first! The obtained path is not the Preference path in iOS system, but the Preference path in Mac system. The enumeration does not provide the Preference path. I guess the main reason is that Apple official does not want developers to directly operate the files in this path. I’m going to do things in UserDefaults or something like that.

Data access

After obtaining the path, the data can be accessed. The data structures that can be accessed directly are as follows:

  • NSMutableArray,NSArray
  • NSData,Data,NSMutableData
  • String,NSString
  • NSDictionary,NSMutableDictionary

Take NSDictionary, for example, and the rest is similar

let dicPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("dic").appendingPathExtension("plist")

let dic: [String: Any] = ["Name": "Zhang"."Age": 24]

/ / / write
NSDictionary(dictionary: dic).write(to: dicPath, atomically: true)

/ / / read
let data = NSDictionary(contentsOf: dicPath)
Copy the code

The last

Try harder this week!

Let’s be CoderStar!

A link to the

File System Programming Guide


It is very important to have a technical circle and a group of like-minded people, come to my technical public account, here only talk about technical dry goods.

Wechat official account: CoderStar