I'm managing the database with SQLite in Flutter. I want to enable iCloud backup and restore on the Swift side when called from Flutter. I am using the following source code, but it is not working. What could be the cause? Could you provide a method and countermeasure?
private func saveFileToICloud(fileName: String, localDatabasePath: String, result: @escaping FlutterResult) { guard let containerName = Bundle.main.object(forInfoDictionaryKey: "ICLOUD_CONTAINER_NAME") as? String else { result(FlutterError(code: "NO_ICLOUD_CONTAINER", message: "iCloud container is not available", details: nil)) return } guard let containerURL = FileManager.default.url(forUbiquityContainerIdentifier: containerName) else { result(FlutterError(code: "NO_ICLOUD_CONTAINER", message: "iCloud container is not available", details: nil)) return } let fileURL = containerURL.appendingPathComponent(fileName) let sourceURL = URL(fileURLWithPath: localDatabasePath) do { if FileManager.default.fileExists(atPath: fileURL.path) { try FileManager.default.removeItem(at: fileURL) } try FileManager.default.copyItem(at: sourceURL, to: fileURL) result("File saved successfully to iCloud: \(fileURL.path)") } catch { result(FlutterError(code: "WRITE_ERROR", message: "Failed to write file to iCloud", details: error.localizedDescription)) } } private func readFileFromICloud(fileName: String, localDatabasePath: String, result: @escaping FlutterResult) { let containerName = ProcessInfo.processInfo.environment["ICLOUD_CONTAINER_NAME"] guard let containerURL = FileManager.default.url(forUbiquityContainerIdentifier: containerName) else { result(FlutterError(code: "NO_ICLOUD_CONTAINER", message: "iCloud container is not available", details: nil)) return } let fileURL = containerURL.appendingPathComponent(fileName) let sourceURL = URL(fileURLWithPath: localDatabasePath) do { if FileManager.default.fileExists(atPath: sourceURL.path) { try FileManager.default.removeItem(at: sourceURL) } try FileManager.default.copyItem(at: fileURL, to: sourceURL) result("File restored successfully to sqlite: \(sourceURL.path)") } catch { result(FlutterError(code: "READ_ERROR", message: "Failed to read file from iCloud", details: error.localizedDescription)) } }