From eef760fa81d138addc35b38208b09da927f768b4 Mon Sep 17 00:00:00 2001 From: Fernando Trigoso Date: Wed, 7 Aug 2024 13:34:51 -0400 Subject: [PATCH 1/2] [shared_prefs] Fixes get-all when suite name is used In shared_preferences_foundation, fixes getting all preferences when suite name is used. Bug was reading only the standard user defaults. The fix uses suite name when available. --- .../shared_preferences_foundation/CHANGELOG.md | 2 +- .../darwin/Tests/RunnerTests.swift | 14 ++++++++++++++ .../SharedPreferencesPlugin.swift | 2 +- .../shared_preferences_foundation/pubspec.yaml | 2 +- 4 files changed, 17 insertions(+), 3 deletions(-) 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..64694b15be0 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,7 @@ 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, + 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 From f0e9d5fee1f9de16d86aa6c09106e03c3d5d4a5e Mon Sep 17 00:00:00 2001 From: Fernando Trigoso Date: Wed, 7 Aug 2024 16:53:13 -0400 Subject: [PATCH 2/2] Comment to clarify fix --- .../SharedPreferencesPlugin.swift | 6 ++++++ 1 file changed, 6 insertions(+) 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 64694b15be0..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,6 +152,12 @@ public class SharedPreferencesPlugin: NSObject, FlutterPlugin, UserDefaultsApi { var filteredPrefs: [String: Any] = [:] var compatiblePrefs: [String: Any] = [:] let allowSet = allowList.map { Set($0) } + + // 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) {