cesarcarlos
cesarcarlos

Reputation: 1405

iOS - KeyValueStore not working across devices

I have an app that will implement key value store in order to save some variables and sync across devices.

I have a view controller with the following:

var keyStore: NSUbiquitousKeyValueStore?

On viewDidLoad I do

keyStore = NSUbiquitousKeyValueStore()

Then when a user finishes watching a video I run

keyStore?.set(num, forKey: "watched") // where num is a number
keyStore?.synchronize()

In order to check the value of the "watched" variable, I'm running this on didFinishLaunchingWithOptions:

keyStore = NSUbiquitousKeyValueStore()
let val = keyStore?.string(forKey: "watched")
print(val)

If I test the app several times, the value of val changes accordingly. However, when I test on a different device, val returns nul. If I use the application in this new device, the value for "watched" changes but values never sync between the two devices.

I read that there is a Notification Method to check if the stored value changed in real time, but in this case I'm reading the stored value during app launch. Wouldn't that make it unneeded?

Upvotes: 0

Views: 160

Answers (1)

cesarcarlos
cesarcarlos

Reputation: 1405

My bad. Make sure you are logged into Cloud in your simulator.

Upvotes: 0

Related Questions