diff --git a/packages/shared_preferences/shared_preferences_foundation/CHANGELOG.md b/packages/shared_preferences/shared_preferences_foundation/CHANGELOG.md index 1eec9791673..67bc77eea68 100644 --- a/packages/shared_preferences/shared_preferences_foundation/CHANGELOG.md +++ b/packages/shared_preferences/shared_preferences_foundation/CHANGELOG.md @@ -1,5 +1,5 @@ ## NEXT - +* Fixes getting all preferences when suite name is used. * Updates minimum supported SDK version to Flutter 3.19/Dart 3.3. ## 2.5.0 diff --git a/packages/shared_preferences/shared_preferences_foundation/darwin/Tests/RunnerTests.swift b/packages/shared_preferences/shared_preferences_foundation/darwin/Tests/RunnerTests.swift index 88d38022d61..a611563c33d 100644 --- a/packages/shared_preferences/shared_preferences_foundation/darwin/Tests/RunnerTests.swift +++ b/packages/shared_preferences/shared_preferences_foundation/darwin/Tests/RunnerTests.swift @@ -115,6 +115,7 @@ class RunnerTests: XCTestCase { // Async system tests. let emptyOptions = SharedPreferencesPigeonOptions() + let optionsWithSuiteName = SharedPreferencesPigeonOptions(suiteName: "group.example.sharedPreferencesFoundationExample") func testAsyncSetAndGet() throws { let plugin = SharedPreferencesPlugin() @@ -153,6 +154,19 @@ class RunnerTests: XCTestCase { XCTAssertEqual(storedValues["aStringList"] as? [String], ["hello", "world"]) } + + func testAsyncGetAllWithAndWithoutSuiteName() throws { + let plugin = SharedPreferencesPlugin() + + try plugin.set(key: "aKey", value: "hello world", options: emptyOptions) + try plugin.set(key: "aKeySuite", value: "hello world with suite", options: optionsWithSuiteName) + + let storedValues = try plugin.getAll(allowList: nil, options: emptyOptions) + XCTAssertEqual(storedValues["aKey"] as? String, "hello world") + + let storedValuesWithGroup = try plugin.getAll(allowList: nil, options: optionsWithSuiteName) + XCTAssertEqual(storedValuesWithGroup["aKeySuite"] as? String, "hello world with suite") + } func testAsyncGetAllWithAllowList() throws { let plugin = SharedPreferencesPlugin() diff --git a/packages/shared_preferences/shared_preferences_foundation/darwin/shared_preferences_foundation/Sources/shared_preferences_foundation/SharedPreferencesPlugin.swift b/packages/shared_preferences/shared_preferences_foundation/darwin/shared_preferences_foundation/Sources/shared_preferences_foundation/SharedPreferencesPlugin.swift index ae86be7bab2..8446e5c2a27 100644 --- a/packages/shared_preferences/shared_preferences_foundation/darwin/shared_preferences_foundation/Sources/shared_preferences_foundation/SharedPreferencesPlugin.swift +++ b/packages/shared_preferences/shared_preferences_foundation/darwin/shared_preferences_foundation/Sources/shared_preferences_foundation/SharedPreferencesPlugin.swift @@ -152,7 +152,13 @@ public class SharedPreferencesPlugin: NSObject, FlutterPlugin, UserDefaultsApi { var filteredPrefs: [String: Any] = [:] var compatiblePrefs: [String: Any] = [:] let allowSet = allowList.map { Set($0) } - if let appDomain = Bundle.main.bundleIdentifier, + + // Since `getUserDefaults` is initialized with the suite name, it seems redundant to call + // `persistentDomain` with the suite name again. However, it is necessary because + // `dictionaryRepresentation` returns keys from the global domain. + // Also, Apple's docs on `persistentDomain` are incorrect, + // see: https://github.com/feedback-assistant/reports/issues/165 + if let appDomain = options.suiteName ?? Bundle.main.bundleIdentifier, let prefs = try getUserDefaults(options: options).persistentDomain(forName: appDomain) { if let allowSet = allowSet { diff --git a/packages/shared_preferences/shared_preferences_foundation/pubspec.yaml b/packages/shared_preferences/shared_preferences_foundation/pubspec.yaml index f465f9120d1..50b20675b80 100644 --- a/packages/shared_preferences/shared_preferences_foundation/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_foundation/pubspec.yaml @@ -2,7 +2,7 @@ name: shared_preferences_foundation description: iOS and macOS implementation of the shared_preferences plugin. repository: https://github.com/flutter/packages/tree/main/packages/shared_preferences/shared_preferences_foundation issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+shared_preferences%22 -version: 2.5.0 +version: 2.5.1 environment: sdk: ^3.3.0