Continuous app extension update
I've created a notification center app extension that works really great except that I can't keep it up to date UILabel
. The reason I need to do this is because my application is constantly changing the dataset that I want to show in the extension.
I tried using NSUserDefaultsDidChangeNotification
to update data in an extension, but it doesn't work. Here is my code:
Registration for change notification (extension)
- (void)viewDidLoad
{
[super viewDidLoad];
...
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(userDefaultsDidChange:)
name:NSUserDefaultsDidChangeNotification
object:nil];
}
- (void)userDefaultsDidChange:(NSNotification *)notification
{
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.company.app"];
//update label
}
Sending data to the extension
- (void)updateData
{
NSUserDefaults *sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.company.app"];
[sharedDefaults setInteger:seconds forKey:@"seconds"];
...
[sharedDefaults synchronize];
}
I have application groups configured correctly in both the main application and the extension, both referring to the same group.
Does anyone know why this is not working, or if there is another way to do it? Many thanks!
source to share
I ran into the same issue in WatchKit app and NSUserDefaultsDidChangeNotification
really doesn't start in extension for generic NSUserDefaults
.
The best solution I've found is a library called MMWormhole that uses
CFNotificationCenter Darwin Notifications
to achieve this.
source to share