Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread Display Modes
Old Jan 25, 2013, 03:46 AM   #1
macrumors newbie
Join Date: Jan 2013
didReceiveData only getting called once on resuming download

I have a problem which I could really do with some help with please

I am using NSURLconnection to download a large file (27MB). the code work fine when there is no network interruption. In order to allow for network issues and only partially downloaded file I have added code to check to see how much of the file is downloaded and then using a server request to download the missing portion.

The code works as it should IF I download part of file, stop the program running and then run again - the download then commences where it left off and i have complete file.

However if I hit the download button a second time without stopping the program then didReceiveData only gets called once and adds just 200KB to the file and it tells me file has been succesfully downloaded.

Help please - I have spent ages trying to figure out what I'm doing wrong.

Code below:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;

    NSLog(@"Response code = %d",httpResponse.statusCode );
     file = [NSFileHandle fileHandleForUpdatingAtPath:filename] ;// file is in .h

    if (file)   {
       [file seekToEndOfFile];


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

    if (file)  {

        [file seekToEndOfFile];
        NSLog(@"file is %@",file);


    [self.file writeData:data];

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

    if([[NSFileManager defaultManager] fileExistsAtPath:filename])
       [file closeFile];
        file = nil;
        theConnection = nil;
        filename = nil;
        theRequest = nil;
     NSLog(@"Connection failed! Error - %@ %@",
     [error localizedDescription],
     [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

    [file closeFile];
    file = nil;
    theConnection = nil;
    filename = nil;


- (IBAction)downloadFile:(id)sender {

    filename = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:movie1]; // filename is in .h file

    theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:movieDownload1]                                          cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

    NSUInteger downloadedBytes = 0;
    NSFileManager *fm = [NSFileManager defaultManager];
    if ([fm fileExistsAtPath:filename]) {
        NSError *error = nil;
        NSDictionary *fileDictionary = [fm attributesOfItemAtPath:filename error:&error];
        if (!error && fileDictionary)
        downloadedBytes = [fileDictionary fileSize];
    } else {
        [fm createFileAtPath:filename contents:nil attributes:nil];

    if (downloadedBytes > 0) {
         NSString *requestRange = [NSString stringWithFormat:@"bytes=%d-",downloadedBytes];
        [theRequest setValue:requestRange forHTTPHeaderField:@"Range"];

    theConnection = nil;
    theConnection = [NSURLConnection connectionWithRequest:theRequest delegate:self];


Last edited by dejo; Jan 25, 2013 at 08:16 AM. Reason: Please use [code] tags.
NigelCollins is offline   0 Reply With Quote
Old Jan 28, 2013, 06:37 AM   #2
macrumors Demi-God
KoolStar's Avatar
Join Date: Oct 2006
Location: Kentucky
Send a message via AIM to KoolStar Send a message via Skype™ to KoolStar
My best guess is that the delegate is getting lost on the resume. I would check that first.
"Innovation distinguishes between a leader and a follower." : S.J.
KoolStar is offline   0 Reply With Quote

MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Looking for a program called ProfileMenu but can't find download saturnotaku Mac Applications and Mac App Store 1 Mar 31, 2014 07:58 PM
Resuming from Sleep Haswell MBP BlockEight88 MacBook Pro 1 Oct 29, 2013 05:53 PM
iPhone: How to stop music from resuming after call? docgerrard Jailbreaks and iOS Hacks 3 May 19, 2013 04:04 PM
Prevent Programs From Resuming After Kernel Panic ibejohn818 Mac OS X 10.7 Lion 1 Jul 5, 2012 04:20 PM

Forum Jump

All times are GMT -5. The time now is 08:31 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2015, MacRumors.com, LLC