6

I've working with the new Today Extension available on iOS 8. Debugging seems to be very difficult on the device with inconsistent results so I've been using the simulator most of the time.

The extension I'm building is a very simple one that just display a different image on a daily basis, the flow is actually pretty simple:

  • iOS calls widgetPerformUpdateWithCompletionHandler
  • I download the image asynchronously
  • If the image was downloaded successfully I set the appropriate outlet on the storyboard and call the completion block with the constant: NCUpdateResultNewData
  • If an error occurred I call the completion block with the constant: NCUpdateResultFailed

According to Apple's reference documentation every time we call the completion block with the constant NCUpdateResultNewData the widget's snapshot should be updated to the current view, however, this doesn't work all the time, sometimes iOS seems to be using an older snapshot.

The code is straightforward, here's the widgetPerformUpdateWithCompletionHandler code:

-(void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
__weak TodayViewController *weakSelf = self;
NSURL *URL = [NSURL URLWithString:@"http://www.muratekim.com/wp-content/uploads/apple-logo-small.png"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:
                              ^(NSData *data, NSURLResponse *response, NSError *error) {
                                  if (!error) {
                                      UIImage *image = [UIImage imageWithData:data];
                                      weakSelf.imageView.image = image;
                                      weakSelf.img = image;
                                      completionHandler(NCUpdateResultNewData);
                                  }
                                  else {
                                      completionHandler(NCUpdateResultFailed);
                                  }
                              }];
[task resume];
}

Thanks in advance! Ze

Vinh
  • 944
  • 13
  • 34
user361526
  • 3,333
  • 5
  • 25
  • 36
  • 6
    Ok so the problem was the I wasn't updating the UI on the main thread, once I started doing so everything worked as expected – user361526 Sep 11 '14 at 16:02
  • That makes sense. SO you just put the UI code in a main thread block or something to force it to run there? –  May 12 '15 at 08:08
  • Yes, so something like:`dispatch_async(dispatch_get_main_queue(), ^{ })` – Dalmazio Sep 30 '18 at 22:58

0 Answers0