Skip to content

A Swift library that monitors the iCloud account status and responds to synchronization events when using Core Data with CloudKit.

License

Notifications You must be signed in to change notification settings

fatbobman/iCloudSyncStatusKit

Repository files navigation

iCloudSyncStatusKit

A Swift library that monitors the iCloud account status and responds to synchronization events when using Core Data with CloudKit. It leverages the new Observation framework in iOS 17 and macOS 14, with compatibility for older OS versions using ObservableObject.

Features

  • Account Status Monitoring: Check if the iCloud account is available and handle unavailable states.
  • Synchronization Event Handling: Monitor importing, exporting, setup, and idle states during data synchronization.
  • Error Handling: Handle specific CloudKit errors, such as quotaExceeded.
  • Logging Support: Optional logging of synchronization events for debugging purposes.

Requirements

  • Swift 6
  • iOS 14.0 or later
  • macOS 11 or later

Installation

Swift Package Manager

Add the package to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/fatbobman/iCloudSyncStatusKit.git", from: "0.1.0")
]

Or add it via Xcode:

  1. Go to File > Add Packages...
  2. Enter the repository URL: https://github.com/fatbobman/iCloudSyncStatusKit.git
  3. Choose the version and add the package to your project.

Usage

Import the Library

import SyncStatusManager

Initialize SyncStatusManager

@StateObject var syncManager = SyncStatusManager()

Observing Sync Events

You can observe syncEvent to monitor the synchronization status.

SwiftUI View Example

struct ContentView: View {
    @StateObject var syncManager = SyncStatusManager()

    var body: some View {
        VStack {
            Text("Sync Event: \(syncManager.syncEvent)")
            // Your UI components
            Button("Check iCloud Status") {
                Task {
                    let status = await syncManager.validateICloudAvailability { status, error in
                        print("Status: \(status)")
                        if let error = error {
                            print("Error: \(error)")
                        }
                    }
                    if let status = status {
                        print("iCloud Account Status: \(status)")
                    }
                }
            }
        }
    }
}

Checking iCloud Availability

Use the validateICloudAvailability method to check the iCloud account status:

Task {
    let status = await syncManager.validateICloudAvailability { status, error in
        print("Account Status: \(status)")
        if let error = error {
            print("Error: \(error.localizedDescription)")
        }
    }
    if status == .available {
        // Proceed with synchronization
    } else {
        // Handle unavailable iCloud account
    }
}

Handling Quota Exceeded

Provide a quotaExceededHandler when initializing SyncStatusManager:

let syncManager = SyncStatusManager(
    quotaExceededHandler: {
        // Notify the user about the quota issue
    }
)

Logging Synchronization Events

If you want to log synchronization events for debugging purposes, set showEventInLog to true and provide a logger that conforms to LoggerManagerProtocol:

let syncManager = SyncStatusManager(
    logger: YourLoggerInstance, // Conforming to LoggerManagerProtocol
    showEventInLog: true
)

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

Buy Me A Coffee

About

A Swift library that monitors the iCloud account status and responds to synchronization events when using Core Data with CloudKit.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages