Difference between NSUserDefaults
NSUserDefaults
is a unit of memory on any device, so it is completely different in iOS and watchOS, as well in tvOS, etc.
For example, if you run a code that adds a new key called "key1" to NSUserDefaults
on iOS (assuming you are using NSUserDefaults.standardUserDefaults()
) , when you request to read its data on watchOS, it gives you an empty dictionary.
That's because it the first time, you store the dictionary only in iOS App Target and not in a shared space between two devices. When the WatchKit extension wants to use NSUserDefaults
, it reads the (empty) dictionary saved in WatchKit Target and not the (filled) dictionary in iOS Target.
Sharing Data
There are some ways of sharing dictionaries between watchOS and iOS:
1- Using WatchConnectivity
framework in watchOS 2, you could pass data between two targets. You can use sendMessage
or similar functions in WCSession
classes to do so. Just don't forget to add WCSessionDelegate
to your InterfaceController
(in watchOS) or ViewController
(in iOS) classes and app delegates for the app to be able to receive data from WatchConnectivity
.
2- Using online/cloud storages, you could upload the dictionary from iOS App there, and then download it to watchOS App. However, this needs an Internet connection, and is not suitable in many cases, for example if your app does not need to connect to the Internet to do other tasks, and you force the user to connect to the Internet just in order to sync a simple dictionary between the two targets.
3- Using App Groups (if you are using watchOS 1), which I don't describe it more, because it is outdated and you probably don't want to make your app just for watchOS 1.
Conclusion
1- NSUserDefaults
returns two different dictionaries in watchOS and iOS, respectively.
2- To share data between watchOS and iOS, you could use WatchConnectivity
, the Internet (not recommended) and App Groups (in watchOS 1).
More Resources
For more details about WatchConnectivity
, the modern answer to your question, you should check out Apple Documentation in Apple Developer Portal.
Also you should see this WWDC session talking about this great framework included in watchOS 2.