Skip to content

Commit

Permalink
Merge pull request #6527 from cheekiatng/TIMOB-17502
Browse files Browse the repository at this point in the history
[TIMOB-17502] iOS: URLSession DownloadCompleted event now returns file url instead of TiBlob
  • Loading branch information
jonalter committed Jan 7, 2015
2 parents 270e881 + 931f332 commit 01d6e76
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions iphone/Classes/TiApp.m
Original file line number Diff line number Diff line change
Expand Up @@ -607,11 +607,26 @@ - (void)application:(UIApplication *)application handleEventsForBackgroundURLSes

-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
//FunctionName();
TiBlob * downloadedFile =[[[TiBlob alloc] initWithData:[NSData dataWithContentsOfURL:location] mimetype:[Mimetypes mimeTypeForExtension:[location absoluteString]]] autorelease];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithUnsignedInteger:downloadTask.taskIdentifier ],@"taskIdentifier",downloadedFile,@"data", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:kTiURLDownloadFinished object:self userInfo:dict];

//FunctionName();
//copy downloaded file from location to tempFile (in NSTemporaryDirectory), because file in location will be removed after delegate completes
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *destinationFilename = location.lastPathComponent;
NSURL *destinationURL = [[NSURL fileURLWithPath:NSTemporaryDirectory()] URLByAppendingPathComponent:destinationFilename];
if ([fileManager fileExistsAtPath:[destinationURL path]]) {
[fileManager removeItemAtURL:destinationURL error:nil];
}
BOOL success = [fileManager copyItemAtURL:location toURL:destinationURL error:&error];
TiBlob* downloadedData;
if (!success) {
DebugLog(@"Unable to copy temp file. Error: %@", [error localizedDescription]);
downloadedData =[[[TiBlob alloc] initWithData:[NSData dataWithContentsOfURL:location] mimetype:[Mimetypes mimeTypeForExtension:[location absoluteString]]] autorelease];
}
else {
downloadedData = [[[TiBlob alloc] initWithFile:[destinationURL path]] autorelease];
}
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithUnsignedInteger:downloadTask.taskIdentifier ],@"taskIdentifier",downloadedData,@"data", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:kTiURLDownloadFinished object:self userInfo:dict];
}

-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
Expand Down

0 comments on commit 01d6e76

Please sign in to comment.