Reputation: 688
I've made an watchApp in an existing IOS app i've successfully passed data from IOS to WatchOS and receiving the data in watch OS via delegate method as you see in the codes below.
My problem is data is not persistent in WatchOS. anytime i need to use the watchApp i have to open the IOS app first and then it updates the watchApp.
i tried AppGroups but seems like it's not working on watchOS 2.0+ anymore.
so how can i save the data on watchOS and only update it via the updateApplicationContextWithApplicationContext
?
NSMutableDictionary * cardDataForWatch = [[NSMutableDictionary alloc] init];
for (Card * card in self.cards)
{
NSMutableDictionary<NSString *, id> *storedData = [[NSMutableDictionary alloc] init];
Store * store = [StoreHelper getStoreForCard:card];
NSString * color = [ColorHelper hexStringForColor:[[store getColorPalette] getHighlightColor]];
NSString * barcode = [card barcode];
if (color != nil)
{
[storedData setValue:color forKey:@"color"];
}
if (barcode != nil)
{
[storedData setValue:barcode forKey:@"barcode"];
}
UIImageView * imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(0, 0, 100, 90);
[BarcodeRenderer renderBarcode:card to:imageView andLabel:nil];
NSData * barcodeImage = UIImagePNGRepresentation([imageView image]);
[storedData setValue:barcodeImage forKey:@"barcodeImage"];
[cardDataForWatch setValue:storedData forKey:[card store]];
}
@try {
[[WatchSessionManager sharedManager] updateApplicationContextWithApplicationContext:cardDataForWatch error:nil];
then i receive the data in watchOS via the delegate method:
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any])
{
print("🦋 \(applicationContext)")
self.delegate?.dataFromIOS(DataSource(data: applicationContext))
}
Upvotes: 0
Views: 174
Reputation: 264
UserDefaults is available as of watchOS 2 (for storing lightweight data like settings). Core Data is also available.
Upvotes: 1