diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index 236710b59..e3500cef7 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -845,13 +845,13 @@ extension AppDelegate { accountStatsWindowController?.showWindow(self) } - @IBAction func showiCloudDriveMissingAlert(_ sender: Any?) { + @IBAction func showiCloudUnavailableAlert(_ sender: Any?) { // Manual testing for alert in AccountsAddCloudKitWindowController // Check for: // - Expected text // - Button for opening Settings // - Button works - mainWindowController?.presentError(AddCloudKitAccountError.iCloudDriveMissing) + mainWindowController?.presentError(AddCloudKitAccountError.iCloudUnavailable) } } diff --git a/Mac/MainMenu/Base.lproj/MainMenu.xib b/Mac/MainMenu/Base.lproj/MainMenu.xib index 751405811..983e3d887 100644 --- a/Mac/MainMenu/Base.lproj/MainMenu.xib +++ b/Mac/MainMenu/Base.lproj/MainMenu.xib @@ -591,10 +591,10 @@ - + - + diff --git a/Mac/MainMenu/mul.lproj/MainMenu.xcstrings b/Mac/MainMenu/mul.lproj/MainMenu.xcstrings index 41eeaef76..7523718e2 100644 --- a/Mac/MainMenu/mul.lproj/MainMenu.xcstrings +++ b/Mac/MainMenu/mul.lproj/MainMenu.xcstrings @@ -1298,19 +1298,19 @@ } }, "m0T-w3-neT.title" : { - "comment" : "Class = \"NSMenuItem\"; title = \"Show iCloud Drive Missing Alert\"; ObjectID = \"m0T-w3-neT\";", + "comment" : "Class = \"NSMenuItem\"; title = \"Show iCloud Unavailable Alert\"; ObjectID = \"m0T-w3-neT\";", "extractionState" : "extracted_with_value", "localizations" : { "en" : { "stringUnit" : { "state" : "new", - "value" : "Show iCloud Drive Missing Alert" + "value" : "Show iCloud Unavailable Alert" } }, "es" : { "stringUnit" : { "state" : "translated", - "value" : "Mostrar alerta de iCloud Drive no disponible" + "value" : "Mostrar alerta de iCloud no disponible" } } } diff --git a/Mac/Preferences/Accounts/AccountsAddCloudKitWindowController.swift b/Mac/Preferences/Accounts/AccountsAddCloudKitWindowController.swift index be9996ea5..bb56260b6 100644 --- a/Mac/Preferences/Accounts/AccountsAddCloudKitWindowController.swift +++ b/Mac/Preferences/Accounts/AccountsAddCloudKitWindowController.swift @@ -11,6 +11,7 @@ import Account final class AccountsAddCloudKitWindowController: NSWindowController { private weak var hostWindow: NSWindow? + private var isCheckingiCloud = false convenience init() { self.init(windowNibName: "AccountsAddCloudKit") @@ -40,19 +41,29 @@ final class AccountsAddCloudKitWindowController: NSWindowController { } @IBAction func create(_ sender: Any) { + guard !isCheckingiCloud else { + return + } assert(!AccountManager.shared.hasiCloudAccount) - guard AddCloudKitAccountUtilities.isiCloudDriveEnabled else { - presentError(AddCloudKitAccountError.iCloudDriveMissing) - return - } + isCheckingiCloud = true + Task { + defer { + isCheckingiCloud = false + } - assert(hostWindow != nil && window != nil) - guard let hostWindow, let window else { - return - } + guard await AccountManager.shared.isiCloudAvailable() else { + presentError(AddCloudKitAccountError.iCloudUnavailable) + return + } - _ = AccountManager.shared.createAccount(type: .cloudKit) - hostWindow.endSheet(window, returnCode: NSApplication.ModalResponse.OK) + assert(hostWindow != nil && window != nil) + guard let hostWindow, let window else { + return + } + + _ = AccountManager.shared.createAccount(type: .cloudKit) + hostWindow.endSheet(window, returnCode: NSApplication.ModalResponse.OK) + } } } diff --git a/Modules/Account/Sources/Account/AccountManager.swift b/Modules/Account/Sources/Account/AccountManager.swift index 69b8615b9..e03aa170c 100644 --- a/Modules/Account/Sources/Account/AccountManager.swift +++ b/Modules/Account/Sources/Account/AccountManager.swift @@ -187,6 +187,10 @@ import ActivityLog // MARK: - API + public func isiCloudAvailable() async -> Bool { + await CloudKitAccountDelegate.isiCloudAvailable() + } + public func createAccount(type: AccountType) -> Account { if type == .cloudKit { if let existingiCloudAccount = iCloudAccount { diff --git a/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift b/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift index c577e0b06..2289c3720 100644 --- a/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift +++ b/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift @@ -39,10 +39,7 @@ enum CloudKitAccountDelegateError: LocalizedError, Sendable { private let syncDatabase: SyncDatabase - private let container: CKContainer = { - let orgID = Bundle.main.object(forInfoDictionaryKey: "OrganizationIdentifier") as! String - return CKContainer(identifier: "iCloud.\(orgID).NetNewsWire") - }() + private let container = CloudKitAccountDelegate.makeContainer() private let accountZone: CloudKitAccountZone private let articlesZone: CloudKitArticlesZone @@ -113,6 +110,25 @@ enum CloudKitAccountDelegateError: LocalizedError, Sendable { Self.logger.debug("CloudKitAccountDelegate: \(#function, privacy: .public) did complete") } + nonisolated static func makeContainer() -> CKContainer { + let orgID = Bundle.main.object(forInfoDictionaryKey: "OrganizationIdentifier") as! String + return CKContainer(identifier: "iCloud.\(orgID).NetNewsWire") + } + + /// False when there’s no iCloud account, the account is restricted, or iCloud is turned off for this app. + /// True when the status check itself fails, since sync reports its own errors. + nonisolated static func isiCloudAvailable() async -> Bool { + guard let status = try? await makeContainer().accountStatus() else { + return true + } + switch status { + case .noAccount, .restricted: + return false + default: + return true + } + } + // CKAccountChanged can arrive on any thread. @objc nonisolated func handleCKAccountChanged(_ note: Notification) { Task { @MainActor in diff --git a/Modules/CloudKitSync/Sources/CloudKitSync/CloudKitError.swift b/Modules/CloudKitSync/Sources/CloudKitSync/CloudKitError.swift index 0f3f7dc48..b572ec87e 100644 --- a/Modules/CloudKitSync/Sources/CloudKitSync/CloudKitError.swift +++ b/Modules/CloudKitSync/Sources/CloudKitSync/CloudKitError.swift @@ -60,7 +60,7 @@ public final class CloudKitError: LocalizedError, Sendable { case .networkFailure: return NSLocalizedString("Network Failure: the internet connection appears to be offline.", comment: "Known iCloud Error") case .notAuthenticated: - return NSLocalizedString("Not Authenticated: to use the iCloud account, you must enable iCloud Drive. Go to device Settings, sign in to iCloud, then in the app settings, be sure the iCloud Drive feature is enabled.", comment: "Known iCloud Error") + return NSLocalizedString("Not Authenticated: to use the iCloud account, sign in to iCloud in Settings and make sure iCloud is turned on for NetNewsWire.", comment: "Known iCloud Error") case .operationCancelled: return NSLocalizedString("Operation Cancelled: the operation was explicitly canceled.", comment: "Known iCloud Error") case .partialFailure: @@ -68,7 +68,7 @@ public final class CloudKitError: LocalizedError, Sendable { case .participantMayNeedVerification: return NSLocalizedString("Participant May Need Verification: you are not a member of the share.", comment: "Known iCloud Error") case .permissionFailure: - return NSLocalizedString("Permission Failure: to use this app, you must enable iCloud Drive. Go to device Settings, sign in to iCloud, then in the app settings, be sure the iCloud Drive feature is enabled.", comment: "Known iCloud Error") + return NSLocalizedString("Permission Failure: to use the iCloud account, sign in to iCloud in Settings and make sure iCloud is turned on for NetNewsWire.", comment: "Known iCloud Error") case .quotaExceeded: return NSLocalizedString("Quota Exceeded: saving would exceed your current iCloud storage quota.", comment: "Known iCloud Error") case .referenceViolation: diff --git a/Modules/CloudKitSync/Sources/CloudKitSync/CloudKitZone.swift b/Modules/CloudKitSync/Sources/CloudKitSync/CloudKitZone.swift index 7a72207b4..491c0fa16 100644 --- a/Modules/CloudKitSync/Sources/CloudKitSync/CloudKitZone.swift +++ b/Modules/CloudKitSync/Sources/CloudKitSync/CloudKitZone.swift @@ -26,7 +26,7 @@ public enum CloudKitZoneError: LocalizedError, Sendable { case .userDeletedZone: return NSLocalizedString("The iCloud data was deleted. Please remove the application iCloud account and add it again to continue using the application's iCloud support.", comment: "User deleted zone.") case .corruptAccount: - return NSLocalizedString("There is an unrecoverable problem with your application iCloud account. Please make sure you have iCloud and iCloud Drive enabled in System Preferences. Then remove the application iCloud account and add it again.", comment: "Corrupt account.") + return NSLocalizedString("There is an unrecoverable problem with your application iCloud account. Please make sure you’re signed in to iCloud and iCloud is turned on for NetNewsWire in Settings. Then remove the application iCloud account and add it again.", comment: "Corrupt account.") default: return NSLocalizedString("An unexpected CloudKit error occurred.", comment: "An unexpected CloudKit error occurred.") } diff --git a/Shared/Localizable.xcstrings b/Shared/Localizable.xcstrings index 2d8158eb9..0d6fd5c83 100644 --- a/Shared/Localizable.xcstrings +++ b/Shared/Localizable.xcstrings @@ -283,7 +283,7 @@ "comment" : "Feed finder" }, "Can’t Add iCloud Account" : { - "comment" : "CloudKit account setup failure description — iCloud Drive not enabled." + "comment" : "CloudKit account setup failure description — iCloud not available." }, "Can’t add this feed because of a download error: “%@”" : { "comment" : "Feed finder" @@ -1105,7 +1105,7 @@ "Open Settings" : { "comment" : "Open Settings button" }, - "Open Settings to configure iCloud and enable iCloud Drive." : { + "Open Settings to sign in to iCloud and turn on iCloud for NetNewsWire." : { "comment" : "CloudKit account setup recovery suggestion" }, "Open System Preferences" : { @@ -1114,7 +1114,7 @@ "Open System Settings" : { "comment" : "Open System Settings button" }, - "Open System Settings to configure iCloud and enable iCloud Drive." : { + "Open System Settings to sign in to iCloud and turn on iCloud for NetNewsWire." : { "comment" : "CloudKit account setup recovery suggestion" }, "Open Theme Folder" : { diff --git a/Shared/Settings/AddCloudKitAccount.swift b/Shared/Settings/AddCloudKitAccount.swift index 43017b6a6..81f3fcba0 100644 --- a/Shared/Settings/AddCloudKitAccount.swift +++ b/Shared/Settings/AddCloudKitAccount.swift @@ -13,20 +13,19 @@ import AppKit #if canImport(UIKit) import UIKit #endif -import RSCore enum AddCloudKitAccountError: LocalizedError, RecoverableError, Sendable { - case iCloudDriveMissing + case iCloudUnavailable var errorDescription: String? { - NSLocalizedString("Can’t Add iCloud Account", comment: "CloudKit account setup failure description — iCloud Drive not enabled.") + NSLocalizedString("Can’t Add iCloud Account", comment: "CloudKit account setup failure description — iCloud not available.") } var recoverySuggestion: String? { #if os(macOS) - NSLocalizedString("Open System Settings to configure iCloud and enable iCloud Drive.", comment: "CloudKit account setup recovery suggestion") + NSLocalizedString("Open System Settings to sign in to iCloud and turn on iCloud for NetNewsWire.", comment: "CloudKit account setup recovery suggestion") #else - NSLocalizedString("Open Settings to configure iCloud and enable iCloud Drive.", comment: "CloudKit account setup recovery suggestion") + NSLocalizedString("Open Settings to sign in to iCloud and turn on iCloud for NetNewsWire.", comment: "CloudKit account setup recovery suggestion") #endif } @@ -52,10 +51,6 @@ enum AddCloudKitAccountError: LocalizedError, RecoverableError, Sendable { } struct AddCloudKitAccountUtilities { - static var isiCloudDriveEnabled: Bool { - Platform.deviceHasiCloudAccount - } - @MainActor static func openiCloudSettings() { #if os(macOS) if let url = URL(string: "x-apple.systempreferences:com.apple.preferences.AppleIDPrefPane") { diff --git a/iOS/Account/CloudKitAccountView.swift b/iOS/Account/CloudKitAccountView.swift index f437af5c1..6c91c338e 100644 --- a/iOS/Account/CloudKitAccountView.swift +++ b/iOS/Account/CloudKitAccountView.swift @@ -15,16 +15,20 @@ struct CloudKitAccountView: View { let didAddAccount: () -> Void @Environment(\.dismiss) private var dismiss - @State private var iCloudDriveError: AddCloudKitAccountError? + @State private var iCloudError: AddCloudKitAccountError? + @State private var isCheckingiCloud = false @State private var isShowingHelp = false var body: some View { AccountSetupSheet(accountType: .cloudKit) { Section { Button(NSLocalizedString("Use iCloud", comment: "Use iCloud button")) { - addAccount() + Task { + await addAccount() + } } .frame(maxWidth: .infinity) + .disabled(isCheckingiCloud) } header: { AccountIconHeader(accountType: .cloudKit) } footer: { @@ -33,12 +37,12 @@ struct CloudKitAccountView: View { } } } - .alert(isPresented: isShowingiCloudDriveError, error: iCloudDriveError) { _ in + .alert(isPresented: isShowingiCloudError, error: iCloudError) { _ in Button(NSLocalizedString("Open Settings", comment: "Open Settings button")) { AddCloudKitAccountUtilities.openiCloudSettings() } Button(NSLocalizedString("Cancel", comment: "Cancel button"), role: .cancel) { - iCloudDriveError = nil + iCloudError = nil } } message: { error in Text(verbatim: error.recoverySuggestion ?? "") @@ -48,19 +52,24 @@ struct CloudKitAccountView: View { } } - private var isShowingiCloudDriveError: Binding { + private var isShowingiCloudError: Binding { Binding { - iCloudDriveError != nil + iCloudError != nil } set: { isShowing in if !isShowing { - iCloudDriveError = nil + iCloudError = nil } } } - private func addAccount() { - guard AddCloudKitAccountUtilities.isiCloudDriveEnabled else { - iCloudDriveError = .iCloudDriveMissing + private func addAccount() async { + isCheckingiCloud = true + defer { + isCheckingiCloud = false + } + + guard await AccountManager.shared.isiCloudAvailable() else { + iCloudError = .iCloudUnavailable return }