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
.
- 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.
- Swift 6
- iOS 14.0 or later
- macOS 11 or later
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:
- Go to File > Add Packages...
- Enter the repository URL:
https://github.com/fatbobman/iCloudSyncStatusKit.git
- Choose the version and add the package to your project.
import SyncStatusManager
@StateObject var syncManager = SyncStatusManager()
You can observe syncEvent
to monitor the synchronization status.
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)")
}
}
}
}
}
}
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
}
}
Provide a quotaExceededHandler
when initializing SyncStatusManager
:
let syncManager = SyncStatusManager(
quotaExceededHandler: {
// Notify the user about the quota issue
}
)
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
)
This project is licensed under the MIT License - see the LICENSE file for details.