[英]Sharing data between app extension and conainer app
I'm trying to share data between an app extension and my container app. 我正在尝试在应用扩展程序和容器应用程序之间共享数据。 I setup an app group in the container app and referenced it my app extension. 我在容器应用程序中设置了一个应用程序组,并引用了我的应用程序扩展名。
When my app extension is accessed, I save an object to NSUserDefaults 访问我的应用程序扩展后,我将一个对象保存到NSUserDefaults
//in the app extension
-(void)viewDidLoad {
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.mygroup.com"];
[defaults setObject:[NSDate date] forKey:@"Date"];
[defaults synchronize];
}
and in the container app - when it is launched I setup an observer: 并在容器应用中-启动后,我设置了一个观察器:
// 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");
}
But if my container app was sent to the background, this notification is never called - so I have no way of knowing when the user default was changed. 但是,如果将我的容器应用发送到后台,则永远不会调用此通知-因此,我无法知道何时更改了用户默认设置。
How can I get the info in the container app (In order to update internally)? 如何在容器应用中获取信息(以便在内部更新)?
I might be mistaken but NSNotification run mainly in the main thread, which means that you will never get that notification. 我可能会弄错,但是NSNotification主要在主线程中运行,这意味着您将永远不会收到该通知。
Also you should unsubscribe to any notification when the view is unloaded, in this case I don't think it's a good idea to have a subscription sitting there. 另外,在卸载视图时,您应该取消订阅任何通知,在这种情况下,我认为在其中放置订阅不是一个好主意。
What I'd do in this case is just check the NSUserDefaults every time the app awakens (there are Delegate methods that you can activate on AppDelegate such as: "applicationDidBecomeActive" 在这种情况下,我要做的就是在每次应用唤醒时检查NSUserDefaults(有一些Delegate方法可以在AppDelegate上激活,例如:“ applicationDidBecomeActive”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.