I am trying to share data between my iOS and WatchKit extension using a common module contained in an embedded framework. The common module class has shared data held in an NSUserDefault with an App Group.
However what I write to the App Group share in the iOS app is not seen when reading from the WatchKit extension.
I have App Groups enabled with the same group ID in both the targets. The iOS reads and writes without error and if the Watch writes data it also reads it back without error -- but neither see the writes made by the other.
Any thoughts how to resolve greatly appreciated.
public class ShareManager() {
private let sharedKey = “kungFuShare"
public var kungfuFighters:Array<KungFu> = [];
public init() {
readFromShare()
}
public func readFromShare() {
let myData = NSUserDefaults(suiteName: "group.com.27feathers.kungfu"
if let rawData: NSData = myData?.objectForKey(sharedKey) as? NSData
{
var myData: AnyObject? = NSKeyedUnarchiver.unarchiveObjectWithData(rawData);
self.kungfuFighters = myData as? [KungFu] ?? []
}
}
public func writeToShare() {
let myData = NSUserDefaults(suiteName: "group.com.27feathers.kungfu")
let saveData = NSKeyedArchiver.archivedDataWithRootObject(self.kungfuFighters)
myData?.setObject(saveData, forKey: sharedKey)
myData?.synchronize() // same behavior with or without this
}
}
public class KungFu: NSObject, NSCoding {
public var fighterName:String
public init(fighterName:String) {
self.fighterName = fighterName
}
required public init(coder: NSCoder) {
self.fighterName = coder.decodeObjectForKey("fighterName")! as String
super.init()
}
public func encodeWithCoder(coder: NSCoder) {
coder.encodeObject(self.fighterName, forKey: "fighterName")
}
}