Skip to content

Commit

Permalink
ios. minior corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrebnov committed Jan 22, 2014
1 parent 5417404 commit 6109cfb
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions src/ios/BackgroundDownload.m
Expand Up @@ -26,10 +26,6 @@ @implementation BackgroundDownload {
@synthesize session;
@synthesize downloadTask;

//https://developer.apple.com/library/IOs/samplecode/SimpleBackgroundTransfer/Introduction/Intro.html
// https://www.captechconsulting.com/blog/nicholas-cipollina/ios-7-tutorial-series-nsurlsession
// http://www.shinobicontrols.com/blog/posts/2013/09/20/ios7-day-by-day-day-1-nsurlsession/
//http://stackoverflow.com/questions/13996621/downloading-multiple-files-in-batches-in-ios
- (void)startAsync:(CDVInvokedUrlCommand*)command
{
self.downloadUri = [command.arguments objectAtIndex:0];
Expand Down Expand Up @@ -76,19 +72,19 @@ - (void)stop:(CDVInvokedUrlCommand*)command
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Arg was null"];
}

[self.downloadTask cancel];
[downloadTask cancel];

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
int64_t progress = 100 * totalBytesWritten / totalBytesExpectedToWrite;
NSMutableDictionary* progressObj = [NSMutableDictionary dictionaryWithCapacity:1];
[progressObj setObject:[NSNumber numberWithInteger:progress] forKey:@"progress"];
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:progressObj];
result.keepCallback = [NSNumber numberWithInteger: TRUE];
[self.commandDelegate sendPluginResult:result callbackId:self.callbackId];
int64_t progress = 100 * totalBytesWritten / totalBytesExpectedToWrite;

NSMutableDictionary* progressObj = [NSMutableDictionary dictionaryWithCapacity:1];
[progressObj setObject:[NSNumber numberWithInteger:progress] forKey:@"progress"];
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:progressObj];
result.keepCallback = [NSNumber numberWithInteger: TRUE];
[self.commandDelegate sendPluginResult:result callbackId:self.callbackId];
}

-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
Expand All @@ -100,6 +96,8 @@ -(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompl
if (error != nil) {
if ((error.code == -999)) {
NSData* resumeData = [[error userInfo] objectForKey:NSURLSessionDownloadTaskResumeData];
// resumeData is available only if operation was terminated by the system (no connection or other reason)
// this happens when application is closed when there is pending download, so we try to resume it
if (resumeData != nil) {
ignoreNextError = YES;
[downloadTask cancel];
Expand All @@ -118,11 +116,8 @@ -(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompl

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
NSFileManager *fileManager = [NSFileManager defaultManager];

NSError *error;

[fileManager removeItemAtPath:self.targetFile error:NULL];
//BOOL success =
[fileManager copyItemAtPath:[location absoluteString] toPath:self.targetFile error:&error];
[fileManager removeItemAtPath:_targetFile error: nil];
[fileManager createFileAtPath:_targetFile contents:[fileManager contentsAtPath:[location path]] attributes:nil];
}
@end

0 comments on commit 6109cfb

Please sign in to comment.