freshking
freshking

Reputation: 1864

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!

Upvotes: 3

Views: 839

Answers (1)

monchote
monchote

Reputation: 3470

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.

Upvotes: 2

Related Questions