User defaults stored in the cloud, automatically syncs `UserDefaults` values that use a key with a specified prefix to the cloud.

What's New

Initial checkin.



CloudUserDefaults automatically syncs UserDefaults values that use a key with a specified prefix to the cloud. Behind the scenes it listens to system events and when a UserDefaults key with the given prefix is set all the user's devices are automatically synced with the new value. For example, if you choose cloud_ as the prefix UserDefaults.standard.set(1, forKey: "cloud_count") is set on all the user's devices.

This is based on Mugunth Kumar's elegant solution MKiCloudSync an Objective-C GitHub repository that is now archived. Thanks to Paul Hudson for suggesting MKiCloudSync.


Swift Package Manager

If you are using Xcode 11 or later:

  1. Click File
  2. Swift Packages
  3. Add Package Dependency...
  4. Specify the git URL for CloudUserDefaults.


Copy CloudUserDefaults.swift to your project


In Xcode, select your project, select your target, click Signing & Capabilities, click + Capability, select iCloud. Check the Key-value storage checkbox.

NOTE iCloud events are not sent to the simulator.


Create an instance of CloudUserDefaults some place it will stay in scope (e.g. in your AppDelegate) and call start with a prefix of your choosing e.g.

import CloudUserDefaults
let cloudUserDefaults = CloudUserDefaults()
cloudUserDefaults.start(prefix: "cloud_")

Whenever a UserDefaults key starts with cloud_ it is automatically synced to all the user's devices e.g.

UserDefaults.standard.set(42, forKey: "cloud_answer") // synced to cloud
UserDefaults.standard.set(42, forKey: "answer")       // local

Subscribe to CloudUserDefaults.cloudSyncNotification if you want to be notified when user defaults from another device are delivered e.g.

 selector: #selector(cloudUpdate(notification:)),
 name: CloudUserDefaults.cloudSyncNotification,
 object: nil)


Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.




  • Swift Tools 4.2.0
View More Packages from this Author


  • None
Last updated: Sat Mar 25 2023 06:16:46 GMT-0500 (GMT-05:00)