Search code examples
iosxcodecocoa-touchios8ios8-today-widget

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).


Solution

  • 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/