Today is an extension for iOS 8 when widgetPerformUpdateWithCompletionHandler includes an asynchronous package

I am working with the new Today Extension, available on iOS 8. Debugging seems very difficult on a device with inconsistent results, so most of the time I use a simulator.

The extension I create is very simple, which displays a different image on a daily basis, the stream is actually quite simple:

  • iOS calls widgetPerformUpdateWithCompletionHandler
  • I upload the image asynchronously
  • If the image was successfully uploaded, I installed the appropriate socket in the storyboard and call the completion block with a constant: NCUpdateResultNewData li>
  • If an error occurs, I call the termination block with a constant: NCUpdateResultFailed

According to Apple reference documentation, every time we call a termination block with the constant NCUpdateResultNewData, the widget snapshot should be updated to the current view, however this does not work all the time, sometimes iOS seems to use the old snapshot.

The code is simple, here is 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! See

+5
source share

Source: https://habr.com/ru/post/1202341/


All Articles