[英]How to communicate between iOS App Containing Extension and Extension (not Host App)
[英]How to create/use a shared app group container as cache storage between containing app and it's extension in ios
應用程序及其擴展之間的數據共享通過常見的AppGroup進行。 我可以使用共享應用程序組容器作為持久數據/文件存儲。 如何在包含應用程序及其擴展程序之間創建/使用共享應用程序組容器作為緩存存儲? 這甚至可能嗎?
是的,你走在正確的軌道上。 這個有可能。
在主應用程序目標的Capabilities
選項卡和WatchKit Extension的目標中,添加一個應用程序組,例如名稱為group.myWatchDefaults
。
然后你可以訪問這樣的默認值:
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.myWatchDefaults"];
要添加數據:
[defaults setObject:[NSNumber numberWithBool:true] forKey:@"myBoolVariable"];
要檢索數據:
bool watchAppOpenedBefore = [[defaults objectForKey:@"myBoolVariable"] boolValue];
確保在更改默認值后使用synchronize
:
[defaults synchronize];
有時,使用應用程序組在Watch應用程序與其父應用程序之間同步NSUserDefaults時,使用Xcode的模擬器時無效。 但是,它適用於實際設備(iPhone和Apple Watch)。 因此,這似乎是模擬器中的一個錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.