CloudDrive

public final class CloudDrive : @unchecked Sendable

SwiftCloudDrive

  • Types of storage available

    See more

    Declaration

    Swift

    public enum Storage
  • The type of storage used (eg iCloud, local)

    Declaration

    Swift

    public let storage: Storage
  • Pass in nil to get the default container. Eg. “iCloud.my.company.app”

    Declaration

    Swift

    public var ubiquityContainerIdentifier: String? { get }
  • The path of the directory for this drive, relative to the root of the drive

    Declaration

    Swift

    public let relativePathToRoot: String
  • Set this to receive notification of changes in the cloud drive.

    Declaration

    Swift

    public var observer: CloudDriveObserver?
  • Optional conflict resolution. If not set, the most recent version wins, and others are deleted.

    Declaration

    Swift

    public var conflictResolver: CloudDriveConflictResolver?
  • If the user is signed in to iCloud, this should be true. Otherwise false. When iCloud is not used, it is always true

    Declaration

    Swift

    public var isConnected: Bool { get }
  • Undocumented

    Declaration

    Swift

    public let rootDirectory: URL

Init

  • Pass in the type of storage (eg iCloud container), and an optional path relative to the root directory where the drive will be anchored.

    Declaration

    Swift

    public init(storage: Storage, relativePathToRoot: String = "") async throws
  • Pass in the container id, but also an optional root direcotry. All relative paths will then be relative to this root.

    Declaration

    Swift

    public convenience init(ubiquityContainerIdentifier: String? = nil, relativePathToRootInContainer: String = "") async throws

File Operations

  • fileExists(at:) Asynchronous

    Returns whether the file exists. If it is a directory, returns false

    Declaration

    Swift

    public func fileExists(at path: RootRelativePath) async throws -> Bool
  • directoryExists(at:) Asynchronous

    Returns whether the directory exists

    Declaration

    Swift

    public func directoryExists(at path: RootRelativePath) async throws -> Bool
  • createDirectory(at:) Asynchronous

    Creates a directory in the cloud. Always creates intermediate directories if needed.

    Declaration

    Swift

    public func createDirectory(at path: RootRelativePath) async throws
  • Returns the contents of a directory. It doesn’t recurse into subdirectories

    Declaration

    Swift

    public func contentsOfDirectory(at path: RootRelativePath, includingPropertiesForKeys keys: [URLResourceKey]? = nil, options mask: FileManager.DirectoryEnumerationOptions = []) async throws -> [URL]
  • removeDirectory(at:) Asynchronous

    Removes a directory at the path passed

    Declaration

    Swift

    public func removeDirectory(at path: RootRelativePath) async throws
  • removeFile(at:) Asynchronous

    Removes a file at the path passed. If there is no file, or there is a directory, it gives an error

    Declaration

    Swift

    public func removeFile(at path: RootRelativePath) async throws
  • upload(from:to:) Asynchronous

    Copies a file from outside the container, into the container. If there is a file already at the destination it will give an error and fail.

    Declaration

    Swift

    public func upload(from fromURL: URL, to path: RootRelativePath) async throws
  • download(from:toURL:) Asynchronous

    Attempts to copy a file inside the container out to a file URL not in the cloud.

    Declaration

    Swift

    public func download(from path: RootRelativePath, toURL: URL) async throws
  • copy(from:to:) Asynchronous

    Copies within the container.

    Declaration

    Swift

    public func copy(from source: RootRelativePath, to destination: RootRelativePath) async throws
  • readFile(at:) Asynchronous

    Reads the contents of a file in the cloud, returning it as data.

    Declaration

    Swift

    public func readFile(at path: RootRelativePath) async throws -> Data
  • writeFile(with:at:) Asynchronous

    Writes the contents of a file. If the file doesn’t exist, it will be created. If it already exists, it will be overwritten.

    Declaration

    Swift

    public func writeFile(with data: Data, at path: RootRelativePath) async throws
  • updateFile(at:in:) Asynchronous

    Make any change to the file contents desired for the path given. Can be used for in-place updates.

    Declaration

    Swift

    public func updateFile(at path: RootRelativePath, in block: @Sendable @escaping (URL) throws -> Void) async throws
  • readFile(at:in:) Asynchronous

    As updateFile, but coordinated for reading.

    Declaration

    Swift

    public func readFile(at path: RootRelativePath, in block: @Sendable @escaping (URL) throws -> Void) async throws