-
Notifications
You must be signed in to change notification settings - Fork 73
/
BHDownload.m
45 lines (42 loc) · 1.66 KB
/
BHDownload.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// BHDownload.m
// DIYTableView
//
// Created by BandarHelal on 12/01/1442 AH.
// Copyright © 1442 BandarHelal. All rights reserved.
//
#import "BHDownload.h"
@implementation BHDownload
- (instancetype)init {
self = [super init];
if (self) {
self.Session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
}
return self;
}
- (void)downloadFileWithURL:(NSURL *)url {
if (url) {
self.fileName = url.absoluteString.lastPathComponent;
NSURLSessionDownloadTask *downloadTask = [self.Session downloadTaskWithURL:url];
[downloadTask resume];
}
}
- (void)setDelegate:(id)newDelegate {
if (newDelegate) {
delegate = newDelegate;
}
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
float prog = (float)totalBytesWritten / (float)totalBytesExpectedToWrite;
[delegate downloadProgress:prog];
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(nonnull NSURL *)location {
[delegate downloadDidFinish:location Filename:self.fileName];
}
- (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error {
[delegate downloadDidFailureWithError:error];
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
[delegate downloadDidFailureWithError:error];
}
@end