I'm downloading file with AFNetworking, using this code:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:downloadFileUrl]];
AFHTTPRequestOperation * operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentDirectory = [paths objectAtIndex:0];
NSString * targetFileName = [downloadFileUrl lastPathComponent];
NSString * targetPath = [documentDirectory stringByAppendingPathComponent:targetFileName];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:targetPath append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"DownloadDidSuccessNotification" object:nil];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"DownloadDidFailedNotification" object:nil];
}];
[operation start];
I provide the option to user to cancel the download, I do that by this:
[operation cancel];
However, the unfinished downloaded file remains in the directory. Please let me know the solution to delete the unfinished downloaded file.
Try this code. I didn't check.