我在今天的扩展程序中有一个进度视图,显示下载的进度并且它可以正常工作,但这是第一次和下次打开扩展时虽然正在执行下载但是没有更新progressView。
@interface TodayViewController () <NCWidgetProviding>{
NSURLSession * session;
NSString * clipboard;
NSString * jsonTitle;
}
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
@end
- (NSURLSession *) configureDownloadSession {
if (!session) {
NSURLSessionConfiguration * sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier: @"bgSessionConfiguration"];
sessionConfiguration.sharedContainerIdentifier = @"group.myapp";
sessionConfiguration.allowsCellularAccess = YES;
session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
}
return session;
}
session = [self configureDownloadSession];
NSURLSessionDownloadTask * downloadTask = [session downloadTaskWithURL:[NSURL URLWithString:[[[json objectForKey:@"formats"] objectAtIndex:0] objectForKey:@"link"]]];
jsonTitle=[[[json objectForKey:@"formats"] objectAtIndex:0] objectForKey:@"title"];
[downloadTask resume];
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
float progress = (double)totalBytesWritten / (double)totalBytesExpectedToWrite;
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"%f",progress);
self.progressView.progress=progress;
[mySharedDefaults synchronize];
});
}