简体   繁体   English

在App Extension和Conainer App之间共享数据

[英]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.

相关问题 在iOS App和Todays Extension之间共享数据 - Sharing Data Between iOS App and Todays Extension 在iOS8中在App和Extension之间共享核心数据堆栈和数据 - Sharing a Core Data stack and data between App and Extension in iOS8 在 iOS 8 共享扩展和主应用程序之间共享数据 - Sharing data between an iOS 8 share extension and main app 在通知服务扩展和主应用之间共享数据 - Sharing data between an notification service extension and main app 在iOS 8扩展程序中访问核心数据SQL数据库(在App和Widget扩展程序之间共享数据) - Accessing Core Data SQL Database in iOS 8 Extension (Sharing Data Between App and Widget Extension) 在原始 iOS App 和 App Extension 之间共享代码 - Sharing code between original iOS App and App Extension 在iOS Extension和app之间使用UIApplication.shared共享CocoaPod - Sharing CocoaPod using UIApplication.shared between iOS Extension and app iOS之间的应用程序组数据共享 - App Groups Data Sharing Between Applications iOS 主应用程序和共享扩展名之间的iOS共享.h文件 - iOS Sharing .h file between main app and share extension 在Swift中的主应用程序和Today小部件/扩展之间共享UI元素 - Sharing UI elements between main App and Today widget/extension in Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM