Requirements
- A Mac computer
- Xcode
- Internet
Goal
The goal is to create an Objective-C class optionally in a framework which takes an array of URLs, downloads their contents asynchronously, and writes the contents of each URL to a unique file.
Optionally a simple App can be created to use the framework to download the URLs listed in the Resource section and show the downloaded files (images).
Specification
The class should be named URLPuller and should have the following instance methods:
- (void)downloadUrlsAsync:(NSArray*)urls
- (void)waitUntilAllDownloadsFinish
- (NSString*)downloadedPathForURL:(NSURL*)url
Here are the detailed descriptions of each method:
downloadUrlsAsync
- (void)downloadUrlsAsync:(NSArray*)urlStrings
The urlStrings array will contain an array of NSString objects each representing an URL. The method should return immediately, e.g. it’s an asynchronous API call that will run in the background.
The downloaded files can be stored in any directory, where the filename is file name part of the url (after the last /)
waitUntilAllDownloadsFinish
- (void)waitUntilAllDownloadsFinish
Block until all of the URLs have been downloaded.
If this is called while there is no download in progress, it should return immediately. Otherwise, it should block until all the downloads have finished.
downloadedPathForURL
- (NSString *)downloadedPathForURL:(NSString *)url
Return the path where the given url was downloaded to.
If the given url has not been downloaded yet, or was never requested to be downloaded, then it should return nil.
Resources
NSArray *urlArray = @[@”https://upload.wikimedia.org/wikipedia/commons/thumb/0/05/Fxemoji_u1F600.svg/64px-Fxemoji_u1F600.svg.png",@”https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Fxemoji_u1F601.svg/64px-Fxemoji_u1F601.svg.png",@”https://upload.wikimedia.org/wikipedia/commons/thumb/a/aa/Fxemoji_u1F602.svg/64px-Fxemoji_u1F602.svg.png",@”https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Fxemoji_u1F603.svg/64px-Fxemoji_u1F603.svg.png",@”https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Fxemoji_u1F604.svg/64px-Fxemoji_u1F604.svg.png",@”https://upload.wikimedia.org/wikipedia/commons/thumb/d/d7/Fxemoji_u1F605.svg/64px-Fxemoji_u1F605.svg.png",@”https://upload.wikimedia.org/wikipedia/commons/thumb/9/9e/Fxemoji_u1F606.svg/64px-Fxemoji_u1F606.svg.png",@”https://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Fxemoji_u1F609.svg/64px-Fxemoji_u1F609.svg.png",@”https://upload.wikimedia.org/wikipedia/commons/thumb/7/70/Fxemoji_u1F60A.svg/64px-Fxemoji_u1F60A.svg.png",@”https://upload.wikimedia.org/wikipedia/commons/thumb/3/30/Fxemoji_u1F60B.svg/64px-Fxemoji_u1F60B.svg.png"];
Credit
Original link: http://tleyden.github.io/blog/2014/01/13/objective-c-coding-interview-challenge-urlpuller/ Thanks!