I have looked at Apple's documentation and searched a lot, but I can't find any code or specific explanation on how to do this.
I want to send data to the widget when users open a specific viewController and then update the widget's view to show the new data.
I have tried:
- Adding a notification. (Did not fire in widget code when posted from app)
- Having a singleton object in the app and update a property, and fetch data from the widget. (This partially works, but the widget is never updated)
I have handled updating the view in the widget, but this is not called when I want to update the widget (changed some values because of company secrets):
- (void) widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
NSDictionary *data = [[SINGLETON sharedInstance] getData];
if(_data == nil && data == nil) {
_showData = NO;
completionHandler(NCUpdateResultNoData);
} else if(_data != nil && [_data isEqual:data]) {
_showData = YES;
completionHandler(NCUpdateResultNoData);
} else if(data == nil) {
_data = nil;
_showData = NO;
[self updateView];
completionHandler(NCUpdateResultNewData);
} else {
_data = data;
_showData = YES;
[self updateView];
completionHandler(NCUpdateResultNewData);
}
}
[self updateView] is updating the widget's view based on _showData (two different views, one that shows data and one without any data).
Can someone please provide me a simple code example (both app and widget) on how to send data to widget (from host app viewController) and how to update widget's view when data is sent / received?