Disk.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import Foundation
  2. /**
  3. 💾 Disk
  4. Easily work with the file system without worrying about any of its intricacies!
  5. - Save Codable structs, UIImage, [UIImage], Data, [Data] to Apple recommended locations on the user's disk, without having to worry about serialization.
  6. - Retrieve an object from disk as the type you specify, without having to worry about deserialization.
  7. - Remove specific objects from disk, clear entire directories if you need to, check if an object exists on disk, and much more!
  8. - Follow Apple's strict guidelines concerning persistence and using the file system easily.
  9. */
  10. public class Disk {
  11. private init() {}
  12. public enum Directory: Equatable {
  13. /// Only documents and other data that is user-generated, or that cannot otherwise be recreated by your application, should be stored in the <Application_Home>/Documents directory.
  14. /// Files in this directory are automatically backed up by iCloud. To disable this feature for a specific file, use the .doNotBackup(:in:) method.
  15. case documents
  16. /// Data that can be downloaded again or regenerated should be stored in the <Application_Home>/Library/Caches directory. Examples of files you should put in the Caches directory include database cache files and downloadable content, such as that used by magazine, newspaper, and map applications.
  17. /// Use this directory to write any application-specific support files that you want to persist between launches of the application or during application updates. Your application is generally responsible for adding and removing these files. It should also be able to re-create these files as needed because iTunes removes them during a full restoration of the device. In iOS 2.2 and later, the contents of this directory are not backed up by iTunes.
  18. /// Note that the system may delete the Caches/ directory to free up disk space, so your app must be able to re-create or download these files as needed.
  19. case caches
  20. /// Put app-created support files in the <Application_Home>/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.
  21. /// Files in this directory are automatically backed up by iCloud. To disable this feature for a specific file, use the .doNotBackup(:in:) method.
  22. case applicationSupport
  23. /// Data that is used only temporarily should be stored in the <Application_Home>/tmp directory. Although these files are not backed up to iCloud, 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.
  24. /// 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.
  25. case temporary
  26. /// Sandboxed apps that need to share files with other apps from the same developer on a given device can use a shared container along with the com.apple.security.application-groups entitlement.
  27. /// The shared container or "app group" identifier string is used to locate the corresponding group's shared directory.
  28. /// For more details, visit https://developer.apple.com/documentation/foundation/nsfilemanager/1412643-containerurlforsecurityapplicati
  29. case sharedContainer(appGroupName: String)
  30. public var pathDescription: String {
  31. switch self {
  32. case .documents: return "<Application_Home>/Documents"
  33. case .caches: return "<Application_Home>/Library/Caches"
  34. case .applicationSupport: return "<Application_Home>/Library/Application"
  35. case .temporary: return "<Application_Home>/tmp"
  36. case let .sharedContainer(appGroupName): return "\(appGroupName)"
  37. }
  38. }
  39. public static func == (lhs: Directory, rhs: Directory) -> Bool {
  40. switch (lhs, rhs) {
  41. case (.applicationSupport, .applicationSupport),
  42. (.caches, .caches),
  43. (.documents, .documents),
  44. (.temporary, .temporary):
  45. return true
  46. case let (.sharedContainer(appGroupName: name1), .sharedContainer(appGroupName: name2)):
  47. return name1 == name2
  48. default:
  49. return false
  50. }
  51. }
  52. }
  53. }