Skip to content

Commit

Permalink
Added support for iOS suites - this is needed i.e. to share data with…
Browse files Browse the repository at this point in the history
… a notification service extension.
  • Loading branch information
micbis committed Jul 23, 2024
1 parent 33bdae1 commit 5922fe0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions preferences/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ Removes old data with `_cap_` prefix from the Capacitor 2 Storage plugin.
| Prop | Type | Description | Default | Since |
| ----------- | ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------- | ----- |
| **`group`** | <code>string</code> | 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. | <code>CapacitorStorage</code> | 1.0.0 |
| **`suite`** | <code>string</code> | iOS: Set the suite name. If a suite is set, the group is automatically set to NativeStorage. | <code></code> | 6.0.0 |


#### GetResult
Expand Down
20 changes: 18 additions & 2 deletions preferences/ios/Sources/PreferencesPlugin/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,37 @@ 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
}
}
}

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 ""
Expand Down
17 changes: 11 additions & 6 deletions preferences/ios/Sources/PreferencesPlugin/PreferencesPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions preferences/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 5922fe0

Please sign in to comment.