Created
August 30, 2020 08:32
-
-
Save BandarHL/d91f76be4bf5b1f5b8d2e6fe23c77ca5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@protocol BHDownloadDelegate <NSObject> | |
- (void)downloadProgress:(float)progress; | |
- (void)downloadDidFinish:(NSURL *_Nullable)filePath Filename:(NSString *_Nullable)fileName; | |
- (void)downloadDidFailureWithError:(NSError *)error; | |
@end | |
@interface BHDownload : NSObject | |
{ | |
id delegate; | |
} | |
- (void)setDelegate:(id _Nonnull )newDelegate; | |
- (instancetype _Nullable )init; | |
- (void)downloadFileWithURL:(NSURL *_Nullable)url; | |
@property (nonatomic, strong) NSString * _Nullable fileName; | |
@end | |
@interface BHDownload () <NSURLSessionDelegate, NSURLSessionDataDelegate, NSURLSessionDownloadDelegate, NSURLSessionStreamDelegate> | |
@property (nonatomic, strong) NSURLSession * _Nullable Session; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
#import <Foundation/Foundation.h> | |
#import "BHDownload.h" | |
@implementation BHDownload | |
- (instancetype _Nullable )init { | |
self = [super init]; | |
if (self) { | |
self.Session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]]; | |
} | |
return self; | |
} | |
- (void)downloadFileWithURL:(NSURL *_Nullable)url { | |
if (url) { | |
self.fileName = url.absoluteString.lastPathComponent; | |
NSURLSessionDownloadTask *downloadTask = [self.Session downloadTaskWithURL:url]; | |
[downloadTask resume]; | |
} | |
} | |
- (void)setDelegate:(id _Nonnull )newDelegate { | |
if (newDelegate) { | |
delegate = newDelegate; | |
} | |
} | |
- (void)URLSession:(nonnull NSURLSession *)session downloadTask:(nonnull 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:(nonnull NSURLSession *)session downloadTask:(nonnull NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(nonnull NSURL *)location { | |
[delegate downloadDidFinish:location Filename:self.fileName]; | |
} | |
- (void)URLSession:(nonnull NSURLSession *)session didBecomeInvalidWithError:(NSError *)error { | |
[delegate downloadDidFailureWithError:error]; | |
} | |
- (void)URLSession:(nonnull NSURLSession *)session task:(nonnull NSURLSessionTask *)task didCompleteWithError:(NSError *)error { | |
[delegate downloadDidFailureWithError:error]; | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface ViewController () <BHDownloadDelegate> | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
BHDownload *download = [[BHDownload alloc] init]; | |
[download setDelegate:self]; | |
[download downloadFileWithURL:[NSURL URLWithString:@"http://ipv4.download.thinkbroadband.com/5MB.zip"]]; | |
} | |
- (void)downloadProgress:(float)progress { | |
NSLog(@"%f", progress); | |
} | |
- (void)downloadDidFinish:(NSURL *)filePath Filename:(NSString *)fileName { | |
NSLog(@"%@\n%@", filePath, fileName); | |
} | |
- (void)downloadDidFailureWithError:(NSError *)error { | |
NSLog(@"error123: %@", error.description); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment