Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refreshing a Today extension view

I created a today extension for my iOS App. My app is fetching new datas in background and saving it into a shared database in the app group.

How I can make (if it's possible) the extension to update it's view when background fetches of the main app are performed ? If it's not possible, how I can make something equivalent (like regularly updating the extension to check for new datas in the shared database).

like image 672
ThibaultV Avatar asked Oct 22 '14 00:10

ThibaultV


2 Answers

You can use shared NSUserDefaults and observer it.

In your app:

After you update database, execute the below:

NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.yourcompany.sharedDefaults"];
[userDefaults setObject:[NSDate date] forKey:@"updatedDate"];
[userDefaults synchronize];


In your Today Widget:

add a observer in viewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(userDefaultsDidChange:)
                                             name:NSUserDefaultsDidChangeNotification
                                           object:nil];

then

- (void)userDefaultsDidChange:(NSNotification *)notification {
    // check updatedDate and update widget UI
}

refer: http://www.glimsoft.com/06/28/ios-8-today-extension-tutorial/

like image 66
scottphc Avatar answered Sep 26 '22 22:09

scottphc


The system create the ViewController every time, but your singleton-instance in your today widget code keep living during a long period.

like image 36
ivanC Avatar answered Sep 23 '22 22:09

ivanC