FavoritesManager is a Swift package that provides a simple way to manage a list of favorite items using iCloud. It uses NSUbiquitousKeyValueStore to store and synchronize data across devices.
Installation
Swift Package Manager
You can use Swift Package Manager to install FavoritesManager by adding it to your Package.swift:
Click on File > Swift Packages > Add Package Dependency
Enter the URL of the FavoritesManager repository: https://github.com/ezefranca/FavoritesManager.git
Follow the instructions to complete the installation.
To use NSUbiquitousKeyValueStore, you need to enable iCloud for your app in the Apple Developer Portal and create an App ID with iCloud enabled.
Then, follow these steps:
Go to your project in Xcode.
Select your target.
Click on the “Signing & Capabilities” tab.
Click the “+ Capability” button to add a new capability to your project.
Search for “iCloud” and select it.
Select the “Key-value storage” checkbox.
Enter a unique identifier for your key-value store in the “iCloud Containers” section.
Save your changes.
After completing these steps, your app should be able to access iCloud key-value storage.
Usage
WARNING
There is a per-key value size limit of 1 MB, and a maximum of 1024 keys. If you attempt to write data that exceeds these quotas, the write attempt fails and no change is made to your iCloud key-value storage. With NSUbiquitousKeyValueStore you’re limited to 1 MB of total space. Don’t use it to store large amounts of data.
To use FavoritesManager, you need to create a struct or class that conforms to the Codable and Equatable protocols. You can then use it to save, remove, and retrieve favorite items. Here’s a simple example of how you could use FavoritesManager:
import Foundation
import FavoritesManager
struct Person: Codable, Equatable {
let name: String
let age: Int
}
var manager = FavoritesManager<Person>(key: "people")
let person1 = Person(name: "John", age: 25)
let person2 = Person(name: "Mary", age: 30)
let person3 = Person(name: "Alex", age: 35)
FavoritesManager


FavoritesManager
is a Swift package that provides a simple way to manage a list of favorite items using iCloud. It usesNSUbiquitousKeyValueStore
to store and synchronize data across devices.Installation
Swift Package Manager
You can use Swift Package Manager to install
FavoritesManager
by adding it to yourPackage.swift
:Or, add it to your project in Xcode:
https://github.com/ezefranca/FavoritesManager.git
To use
NSUbiquitousKeyValueStore
, you need to enable iCloud for your app in the Apple Developer Portal and create an App ID with iCloud enabled.Then, follow these steps:
Go to your project in Xcode.
After completing these steps, your app should be able to access iCloud key-value storage.
Usage
NSUbiquitousKeyValueStore
you’re limited to 1 MB of total space. Don’t use it to store large amounts of data.To use
FavoritesManager
, you need to create a struct or class that conforms to theCodable
andEquatable
protocols. You can then use it to save, remove, and retrieve favorite items. Here’s a simple example of how you could useFavoritesManager
:Save a favorite element
Save another favorite element
Try to save a duplicated element
Check if an element is favorite
Get all favorite elements
Remove a favorite element
Remove all favorite elements
Contributing
Contributions are very welcome! If you have an idea or a bug report, please open an issue or submit a pull request.
License
FavoritesManager
is available under the MIT license. See the LICENSE file for more info.