Last active
August 15, 2020 23:37
-
-
Save BandarHL/4eeb051e80659ec637ba38a2b2f64fcc 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
#import <Foundation/Foundation.h> | |
#import <UIKit/UIKit.h> | |
@interface DownloadManager : NSObject | |
+ (void)DownloadVideoWithURL:(NSString *)url completionHandler:(void (^)(NSURL *filePath, NSError *error))completionHandler; | |
@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
@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 |
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
@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