特定のファイルのファイルダウンロードの進行状況(受信バイト数)を表示したい。 NSURLSessionDownloadTaskで問題なく動作します。私の質問は、NSURLSessionDataTaskでも同じことを達成したいのです。
NSDataにファイルを受け取り、ドキュメントフォルダーに書き込むコードは次のとおりです。
_NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];
NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithURL:theRessourcesURL
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
if(error == nil)
{
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pathToDownloadTo = [NSString stringWithFormat:@"%@/%@", docsDir, Name];
NSLog(@"SIZE : %@",[NSByteCountFormatter stringFromByteCount:data.length countStyle:NSByteCountFormatterCountStyleFile]);
[data writeToFile:pathToDownloadTo options:NSDataWritingAtomic error:&error];
}
}];
[dataTask resume];
_
書き込みまたはデータタスク完了後(ファイルの受信後)にファイルサイズを取得しています:
NSLog(@"SIZE : %@",[NSByteCountFormatter stringFromByteCount:data.length countStyle:NSByteCountFormatterCountStyleFile]);
しかし、現在のバイト受信ステータスを表示したいのですが、これはNSURLSessionDataTaskで可能ですか?
次のデリゲートを実装する必要があります。
<NSURLSessionDataDelegate, NSURLSessionDelegate, NSURLSessionTaskDelegate>
また、2つのプロパティを作成する必要があります。
@property (nonatomic, retain) NSMutableData *dataToDownload;
@property (nonatomic) float downloadSize;
- (void)viewDidLoad {
[super viewDidLoad];
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];
NSURL *url = [NSURL URLWithString: @"your url"];
NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithURL: url];
[dataTask resume];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
completionHandler(NSURLSessionResponseAllow);
progressBar.progress=0.0f;
_downloadSize=[response expectedContentLength];
_dataToDownload=[[NSMutableData alloc]init];
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
[_dataToDownload appendData:data];
progressBar.progress=[ _dataToDownload length ]/_downloadSize;
}
NSURLSessionDownloadTaskを次のように使用することもできます。 startDownloadメソッドを呼び出します。e.Inファイルでこれを使用します
- (void)startDownload
{
NSString *s;
s = @"http://www.nasa.gov/sites/default/files/styles/1600x1200_autoletterbox/public/pia17474_1.jpg?itok=4fyEwd02";
NSURLSessionDownloadTask *task = [self.session downloadTaskWithURL:[NSURL URLWithString:s]];
[task resume];
}
- (NSURLSession *) configureSession {
NSURLSessionConfiguration *config =
[NSURLSessionConfiguration backgroundSessionConfiguration:@"com.neuburg.matt.ch37backgroundDownload"];
config.allowsCellularAccess = NO;
// ... could set config.discretionary here ...
NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];
return session;
}
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
CGFloat prog = (float)totalBytesWritten/totalBytesExpectedToWrite;
NSLog(@"downloaded %d%%", (int)(100.0*prog));
}
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes {
// unused in this example
}
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
NSData *d = [NSData dataWithContentsOfURL:location];
UIImage *im = [UIImage imageWithData:d];
dispatch_async(dispatch_get_main_queue(), ^{
self.image = im;
});
}
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
NSLog(@"completed; error: %@", error);
}
IOS 11.0およびmacOS 10.13以降、URLSessionTask
(以前のNSURLSessionTask
) ProgressReportingプロトコルを採用 。つまり、 progress
プロパティを使用して、セッションタスクの進行状況を追跡できます。
KVOオブザーバーの使用方法を既に知っていることを期待して、次のようなことができます。
task = session.downloadTask(with: url)
task.resume()
task.progress.addObserver(self, forKeyPath: "fractionCompleted", options: .new, context: &self.progressKVOContext)
と値を観察します:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if context == &self.progressKVOContext, let keyPath = keyPath {
switch keyPath {
case "fractionCompleted":
guard let progress = object as? Progress else {
return
}
DispatchQueue.main.async { [weak self] in
self?.onDownloadProgress?(progress.fractionCompleted)
}
case "isCancelled":
cancel()
default:
break
}
}
else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}
}
import Foundation
import PlaygroundSupport
let page = PlaygroundPage.current
page.needsIndefiniteExecution = true
let url = URL(string: "https://source.unsplash.com/random/4000x4000")!
let task = URLSession.shared.dataTask(with: url) { _, _, _ in
page.finishExecution()
}
// Don't forget to invalidate the observation when you don't need it anymore.
let observation = task.progress.observe(\.fractionCompleted) { progress, _ in
print(progress.fractionCompleted)
}
task.resume()