Skip to content

Instantly share code, notes, and snippets.

@bennythemink
Created July 2, 2015 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennythemink/e1f1081d56c79208f5f8 to your computer and use it in GitHub Desktop.
Save bennythemink/e1f1081d56c79208f5f8 to your computer and use it in GitHub Desktop.
@protocol KmlDownloaderProtocol <NSObject>
- (void) downloadFileFrom:(NSString*)urlString isAsyncDownload:(BOOL)isAsync;
- (NSURL*) urlForKmlFile;
@end
@interface KmlDownloader : NSObject <KmlDownloaderProtocol>
#pragma mark - Methods
+ (KmlDownloader*) sharedKmlDownloader;
- (NSURL*) urlForTestKmlFile;
@end
#import "KmlDownloader.h"
@interface KmlDownloader ()
@property (nonatomic, strong) NSString *nameOnDisk;
@property (nonatomic, strong) NSString *pathToKmlFileOnDisk;
@end
@implementation KmlDownloader
+ (KmlDownloader*) sharedKmlDownloader
{
static KmlDownloader *sharedKmlDownloader;
if (!sharedKmlDownloader)
{
sharedKmlDownloader = [[KmlDownloader alloc] init];
}
return sharedKmlDownloader;
}
- (instancetype) init
{
if (self = [super init])
{
_nameOnDisk = @"SiteKmlFile.kml";
}
return self;
}
/**
Downloads a file from the specified URL string. Can download async or sync.
@param: urlString - The location of the file.
@prarm: isAsync - The download method.
*/
- (void) downloadFileFrom:(NSString*)urlString isAsyncDownload:(BOOL)isAsync
{
if ([urlString length] < 13) // http://a.b.co
{
return;
}
// Construct the file path.
NSURL *url = [NSURL URLWithString:urlString];
self.pathToKmlFileOnDisk = [self pathForFile:self.nameOnDisk];
if (isAsync == YES)
{
// fetch async.
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError == nil)
{
if([data writeToFile:self.pathToKmlFileOnDisk atomically:YES] == NO)
{
NSLog(@"There was a problem saving the Kml file to disk!");
}
}
}];
}
else
{
// Synchronous download of file.
NSData *kmlData = [NSData dataWithContentsOfURL:url];
if (kmlData)
{
if ([kmlData writeToFile:self.pathToKmlFileOnDisk atomically:YES] == NO)
{
NSLog(@"There was a problem saving the Kml file to disk!");
}
}
}
}
/**
Returns the path to the file on disk.
@param: fileName - The name of the file.
*/
- (NSString*) pathForFile:(NSString*)fileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, fileName];
return filePath;
}
/**
Returns the URL for the Kml file on disk.
*/
- (NSURL*) urlForKmlFile
{
if (self.nameOnDisk)
{
// BOOL kmlFileExists = [[NSFileManager defaultManager] fileExistsAtPath:self.pathToKmlFileOnDisk];
return [NSURL URLWithString:self.pathToKmlFileOnDisk];
}
else
{
return nil;
}
}
/**
Returns the path to the test KML file in the main bundle.
*/
- (NSURL*) urlForTestKmlFile
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"kml"];
NSURL *url = [NSURL fileURLWithPath:path];
return url;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment