diff --git a/preferences/README.md b/preferences/README.md index 94a64093e..82352b120 100644 --- a/preferences/README.md +++ b/preferences/README.md @@ -244,6 +244,7 @@ Removes old data with `_cap_` prefix from the Capacitor 2 Storage plugin. | Prop | Type | Description | Default | Since | | ----------- | ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------- | ----- | | **`group`** | string | Set the preferences group. Preferences groups are used to organize key/value pairs. Using the value 'NativeStorage' provides backwards-compatibility with [`cordova-plugin-nativestorage`](https://www.npmjs.com/package/cordova-plugin-nativestorage). WARNING: The `clear()` method can delete unintended values when using the 'NativeStorage' group. | CapacitorStorage | 1.0.0 | +| **`suite`** | string | iOS: Set the suite name. If a suite is set, the group is automatically set to NativeStorage. | | 6.0.0 | #### GetResult diff --git a/preferences/ios/Sources/PreferencesPlugin/Preferences.swift b/preferences/ios/Sources/PreferencesPlugin/Preferences.swift index e24a0ed6d..8b2e45219 100644 --- a/preferences/ios/Sources/PreferencesPlugin/Preferences.swift +++ b/preferences/ios/Sources/PreferencesPlugin/Preferences.swift @@ -5,10 +5,16 @@ public struct PreferencesConfiguration { case named(String), cordovaNativeStorage } + let suite: String let group: Group - public init(for group: Group = .named("CapacitorStorage")) { - self.group = group + public init(for suite: String = "", for group: Group = .named("CapacitorStorage")) { + self.suite = suite + if !suite.isEmpty { + self.group = .cordovaNativeStorage; + } else { + self.group = group + } } } @@ -16,10 +22,20 @@ public class Preferences { private let configuration: PreferencesConfiguration private var defaults: UserDefaults { + if !configuration.suite.isEmpty { + if let i = UserDefaults.init(suiteName: configuration.suite) { + return i + } + } + return UserDefaults.standard } private var prefix: String { + if !configuration.suite.isEmpty { + return "" + } + switch configuration.group { case .cordovaNativeStorage: return "" diff --git a/preferences/ios/Sources/PreferencesPlugin/PreferencesPlugin.swift b/preferences/ios/Sources/PreferencesPlugin/PreferencesPlugin.swift index d7ea0aefc..c7b7db4ee 100644 --- a/preferences/ios/Sources/PreferencesPlugin/PreferencesPlugin.swift +++ b/preferences/ios/Sources/PreferencesPlugin/PreferencesPlugin.swift @@ -19,16 +19,21 @@ public class PreferencesPlugin: CAPPlugin, CAPBridgedPlugin { @objc func configure(_ call: CAPPluginCall) { let group = call.getString("group") + let suite = call.getString("suite") let configuration: PreferencesConfiguration - if let group = group { - if group == "NativeStorage" { - configuration = PreferencesConfiguration(for: .cordovaNativeStorage) + if group == nil, let suite = suite { + configuration = PreferencesConfiguration(for: suite) + } else { + if let group = group, let suite = suite { + if group == "NativeStorage" { + configuration = PreferencesConfiguration(for: suite, for: .cordovaNativeStorage) + } else { + configuration = PreferencesConfiguration(for: suite, for: .named(group)) + } } else { - configuration = PreferencesConfiguration(for: .named(group)) + configuration = PreferencesConfiguration() } - } else { - configuration = PreferencesConfiguration() } preferences = Preferences(with: configuration) diff --git a/preferences/src/definitions.ts b/preferences/src/definitions.ts index 3b690fb1c..6f85f9b52 100644 --- a/preferences/src/definitions.ts +++ b/preferences/src/definitions.ts @@ -13,6 +13,15 @@ export interface ConfigureOptions { * @since 1.0.0 */ group?: string; + + /** + * Set the suite name. + * + * If a suite is set, the group is automatically set to NativeStorage. + * + * @default "" + */ + suite?: string; } export interface GetOptions {