gpt4 book ai didi

swift - 数据持久 WatchOS 4.0

转载 作者:行者123 更新时间:2023-11-28 13:34:11 24 4
gpt4 key购买 nike

我在现有的 IOS 应用程序中制作了一个 watchApp我已经成功地将数据从 IOS 传递到 WatchOS,并通过委托(delegate)方法在 watch OS 中接收数据,如下面的代码所示。

我的问题是数据在 WatchOS 中不持久。每当我需要使用 watchApp 时,我都必须先打开 IOS 应用程序,然后它会更新 watchApp。

我尝试了 AppGroups,但它似乎不再适用于 watchOS 2.0+。

那么我如何在 watchOS 上保存数据并仅通过 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];

然后我通过委托(delegate)方法在 watchOS 中接收数据:

func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any])
{
print("🦋 \(applicationContext)")
self.delegate?.dataFromIOS(DataSource(data: applicationContext))
}

最佳答案

UserDefaults 从 watchOS 2 开始可用(用于存储设置等轻量级数据)。核心数据也可用。

关于swift - 数据持久 WatchOS 4.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56891829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com