Skip to content

Instantly share code, notes, and snippets.

@BandarHL
Last active August 15, 2020 23:37
Show Gist options
  • Save BandarHL/4eeb051e80659ec637ba38a2b2f64fcc to your computer and use it in GitHub Desktop.
Save BandarHL/4eeb051e80659ec637ba38a2b2f64fcc to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface DownloadManager : NSObject
+ (void)DownloadVideoWithURL:(NSString *)url completionHandler:(void (^)(NSURL *filePath, NSError *error))completionHandler;
@end
@implementation DownloadManager
+ (void)DownloadVideoWithURL:(NSString *)url completionHandler:(void (^)(NSURL *filePath, NSError *error))completionHandler {
NSError *err;
NSString *documentsDirectoryURL = [NSString stringWithFormat:@"%@/%@", NSHomeDirectory(), @"Documents"];
NSURL *fileURL = [NSURL URLWithString:url];
NSString *fileName = fileURL.lastPathComponent;
NSString *downloadPath = [documentsDirectoryURL stringByAppendingPathComponent:fileName];
NSURL *downloadPathURL = [NSURL fileURLWithPath:downloadPath];
NSData *dataFile = [NSData dataWithContentsOfURL:fileURL];
if (dataFile) {
[dataFile writeToFile:downloadPath options:nil error:&err];
}
completionHandler(downloadPathURL, err);
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[DownloadManager DownloadVideoWithURL:@"https://ff.com/ff.mp4" completionHandler:^(NSURL *filePath, NSError *error) {
if (error) {
// catch error and handle it
} else {
// no error
}
}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment