Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continuously update app extension

I have created an app extension for the notification center which actually works fine except for I am not able to update its UILabel continuously. The reason I need to do this is because my app has a constantly changing data set which I want to show in the extension.

I have tried using NSUserDefaultsDidChangeNotificationfor updating the data in the extension but it's not working. Here is my code:

Registering 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 extension

- (void)updateData
{
    NSUserDefaults *sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.company.app"];
    [sharedDefaults setInteger:seconds forKey:@"seconds"];
    ...
    [sharedDefaults synchronize];
}

I have App Groups correctly set up in both the main app and the extension who are both accessing the same group.

Does anybody know why this is not working or if there is another method to do this? Thanks a lot!

like image 414
freshking Avatar asked Jan 30 '26 03:01

freshking


1 Answers

I'm facing the same issue in my WatchKit app and NSUserDefaultsDidChangeNotification does indeed not get triggered in the extension for the shared NSUserDefaults.

The best solution I've found is a library called MMWormhole which uses CFNotificationCenter Darwin Notifications to achieve this.

like image 173
monchote Avatar answered Jan 31 '26 21:01

monchote



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!