CB-2190 Make backgroundTaskId apply to downloads as well. Move backgroundTaskId to the delegate.
Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/commit/a588236c Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/tree/a588236c Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/diff/a588236c Branch: refs/heads/master Commit: a588236cbdb3c81e2b38b8bfc036aa89520b28d5 Parents: a1d6fc0 Author: Jan Pittner <[email protected]> Authored: Tue Feb 18 20:44:48 2014 +0100 Committer: Andrew Grieve <[email protected]> Committed: Wed Feb 19 10:38:37 2014 -0500 ---------------------------------------------------------------------- src/ios/CDVFileTransfer.h | 2 +- src/ios/CDVFileTransfer.m | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/blob/a588236c/src/ios/CDVFileTransfer.h ---------------------------------------------------------------------- diff --git a/src/ios/CDVFileTransfer.h b/src/ios/CDVFileTransfer.h index 1ce7abe..07feb85 100644 --- a/src/ios/CDVFileTransfer.h +++ b/src/ios/CDVFileTransfer.h @@ -53,7 +53,6 @@ extern NSString* const kOptionsKeyCookie; AndHttpStatus:(int)httpStatus AndBody:(NSString*)body; @property (readonly) NSMutableDictionary* activeTransfers; -@property (nonatomic, assign) UIBackgroundTaskIdentifier backgroundTaskID; @end @class CDVFileTransferEntityLengthRequest; @@ -65,6 +64,7 @@ extern NSString* const kOptionsKeyCookie; @property (strong) NSMutableData* responseData; // atomic @property (nonatomic, strong) NSDictionary* responseHeaders; +@property (nonatomic, assign) UIBackgroundTaskIdentifier backgroundTaskID; @property (nonatomic, strong) CDVFileTransfer* command; @property (nonatomic, assign) CDVFileTransferDirection direction; @property (nonatomic, strong) NSURLConnection* connection; http://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer/blob/a588236c/src/ios/CDVFileTransfer.m ---------------------------------------------------------------------- diff --git a/src/ios/CDVFileTransfer.m b/src/ios/CDVFileTransfer.m index c5c7502..9125e2f 100644 --- a/src/ios/CDVFileTransfer.m +++ b/src/ios/CDVFileTransfer.m @@ -220,12 +220,6 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream) CFStreamCreateBoundPair(NULL, &readStream, &writeStream, kStreamBufferSize); [req setHTTPBodyStream:CFBridgingRelease(readStream)]; - self.backgroundTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ - [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskID]; - self.backgroundTaskID = UIBackgroundTaskInvalid; - NSLog(@"Background task to upload media finished."); - }]; - [self.commandDelegate runInBackground:^{ if (CFWriteStreamOpen(writeStream)) { NSData* chunks[] = {postBodyBeforeFile, fileData, postBodyAfterFile}; @@ -334,6 +328,10 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream) } CDVFileTransferDelegate* delegate = [self delegateForUploadCommand:command]; [NSURLConnection connectionWithRequest:req delegate:delegate]; + // sets a background task ID for the transfer object. + delegate.backgroundTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ + [delegate cancelTransfer:delegate.connection]; + }]; if (activeTransfers == nil) { activeTransfers = [[NSMutableDictionary alloc] init]; @@ -410,6 +408,9 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream) delegate.target = [targetURL absoluteString]; delegate.targetURL = targetURL; delegate.trustAllHosts = trustAllHosts; + delegate.backgroundTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ + [delegate cancelTransfer:delegate.connection]; + }]; delegate.connection = [NSURLConnection connectionWithRequest:req delegate:delegate]; @@ -560,8 +561,8 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream) [command.activeTransfers removeObjectForKey:objectId]; // remove background id task in case our upload was done in the background - [[UIApplication sharedApplication] endBackgroundTask:self.command.backgroundTaskID]; - self.command.backgroundTaskID = UIBackgroundTaskInvalid; + [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskID]; + self.backgroundTaskID = UIBackgroundTaskInvalid; } - (void)removeTargetFile @@ -574,6 +575,11 @@ static CFIndex WriteDataToStream(NSData* data, CFWriteStreamRef stream) - (void)cancelTransfer:(NSURLConnection*)connection { [connection cancel]; + + CDVFileTransferDelegate* delegate = [self.command.activeTransfers objectForKey:self.objectId]; + [[UIApplication sharedApplication] endBackgroundTask:delegate.backgroundTaskID]; + delegate.backgroundTaskID = UIBackgroundTaskInvalid; + [self.command.activeTransfers removeObjectForKey:self.objectId]; [self removeTargetFile]; }
