2727
2828import Foundation
2929
30+ /// ### UserDefault
31+ /// A defined structure for storing information in `NSUserDefaults` and `NSUbiquitousKeyValueStore`.
32+ /// The `timestamp` and optional `build` variables allow for comparisson during syncing.
3033public struct UserDefault {
3134 public var value : NSObject
3235 public var timestamp : NSDate = NSDate ( )
@@ -41,6 +44,8 @@ public struct UserDefault {
4144 }
4245}
4346
47+ /// ### UserDefaults
48+ /// A protocol declaring dictionary conformance. These methods are taken from `NSUbiquitousKeyValueStore`.
4449public protocol UserDefaults {
4550 func dictionaryForKey( aKey: String ) -> [ String : AnyObject ] ?
4651 func setDictionary( aDictionary: [ String : AnyObject ] ? , forKey aKey: String )
@@ -104,6 +109,8 @@ public protocol KeyValueUbiquityContainerDelegate {
104109 func didSetUserDefault( userDefault: UserDefault , forKey key: String )
105110}
106111
112+ /// ### KeyValueUbiquityContainer
113+ /// A subclass of `UbiquityContainer` that manages access to a `NSUbiquitousKeyValueStore` instance.
107114public class KeyValueUbiquityContainer : UbiquityContainer {
108115 public struct Keys {
109116 static let value = " value "
@@ -118,9 +125,7 @@ public class KeyValueUbiquityContainer: UbiquityContainer {
118125 didSet {
119126 if let keyValueStore = self . keyValueStore {
120127 NSNotificationCenter . defaultCenter ( ) . addObserver ( self , selector: #selector( KeyValueUbiquityContainer . nsUbiquitiousKeyValueStoreDidChangeExternally ( _: ) ) , name: NSUbiquitousKeyValueStoreDidChangeExternallyNotification, object: keyValueStore)
121- if !keyValueStore. synchronize ( ) {
122- Logger . log ( . Error, message: " Failed to synchronize Ubiquitious Key Value Store " , error: nil , type: self . dynamicType)
123- }
128+ keyValueStore. synchronize ( )
124129 }
125130 }
126131 }
@@ -132,9 +137,10 @@ public class KeyValueUbiquityContainer: UbiquityContainer {
132137 }
133138
134139 public override func ubiquityStateDidChange( oldState: UbiquityState , newState: UbiquityState ) {
135- if oldState == . Available && newState != . Available {
140+ switch newState {
141+ case . Disabled:
136142 self . keyValueStore = nil
137- } else if oldState != . Available && newState == . Available {
143+ default :
138144 self . keyValueStore = NSUbiquitousKeyValueStore . defaultStore ( )
139145 }
140146 }
@@ -188,17 +194,26 @@ public class KeyValueUbiquityContainer: UbiquityContainer {
188194 default : break
189195 }
190196
197+ Logger . verbose ( " \( keyValueStore. dictionaryRepresentation) " )
191198 }
192199}
193200
194201public extension NSUserDefaults {
195202 public static var ubiquityUserDefaults : KeyValueUbiquityContainer = KeyValueUbiquityContainer ( )
196203
197204 public static func setUserDefault( userDefault: UserDefault , forKey key: String ) {
198- NSUserDefaults . ubiquityUserDefaults. keyValueStore? . setUserDefault ( userDefault, forKey: key)
205+ if let keyValueStore = NSUserDefaults . ubiquityUserDefaults. keyValueStore {
206+ keyValueStore. setUserDefault ( userDefault, forKey: key)
207+ } else {
208+ NSUserDefaults . standardUserDefaults ( ) . setUserDefault ( userDefault, forKey: key)
209+ }
199210 }
200211
201212 public static func userDefaultForKey( key: String ) -> UserDefault ? {
202- return NSUserDefaults . ubiquityUserDefaults. keyValueStore? . userDefaultForKey ( key)
213+ if let userDefault = NSUserDefaults . ubiquityUserDefaults. keyValueStore? . userDefaultForKey ( key) {
214+ return userDefault
215+ }
216+
217+ return NSUserDefaults . standardUserDefaults ( ) . userDefaultForKey ( key)
203218 }
204219}
0 commit comments