[英]Sharing data between app extension and conainer app
我正在嘗試在應用擴展程序和容器應用程序之間共享數據。 我在容器應用程序中設置了一個應用程序組,並引用了我的應用程序擴展名。
訪問我的應用程序擴展后,我將一個對象保存到NSUserDefaults
//in the app extension
-(void)viewDidLoad {
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.mygroup.com"];
[defaults setObject:[NSDate date] forKey:@"Date"];
[defaults synchronize];
}
並在容器應用中-啟動后,我設置了一個觀察器:
// in the container app
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDefaultChanged:) name:NSUserDefaultsDidChangeNotification object:nil];
}
-(void)userDefaultChanged:(NSNotification *)note
{
NSLog(@"Changed");
}
但是,如果將我的容器應用發送到后台,則永遠不會調用此通知-因此,我無法知道何時更改了用戶默認設置。
如何在容器應用中獲取信息(以便在內部更新)?
我可能會弄錯,但是NSNotification主要在主線程中運行,這意味着您將永遠不會收到該通知。
另外,在卸載視圖時,您應該取消訂閱任何通知,在這種情況下,我認為在其中放置訂閱不是一個好主意。
在這種情況下,我要做的就是在每次應用喚醒時檢查NSUserDefaults(有一些Delegate方法可以在AppDelegate上激活,例如:“ applicationDidBecomeActive”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.