title: NSURLConnection download large file (>40MB)
body: <p>I need to download large file (i.e. > 40MB) to my application from server, this file will be ZIP or PDF. I achieved it using <strong>NSURLConnection</strong>, that works well if the file is smaller other wise it download a part of the fill and the execution has stopped. for example I  tried to download 36MB PDF file, but but 16MB only downloaded. pls help me to know the reason? how to fix it?  </p>
  
  <p>FYI:
  Implementation File</p>
  
  <pre><code>#import "ZipHandlerV1ViewController.h"
  @implementation ZipHandlerV1ViewController
  - (void)dealloc
  {
      [super dealloc];
  }
  
  - (void)didReceiveMemoryWarning
  {
      [super didReceiveMemoryWarning];
  }
   - (void)viewDidLoad
   {
       UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
       [mainView setBackgroundColor:[UIColor darkGrayColor]];
  
       UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
       [downloadButton setFrame:CGRectMake(50, 50, 150, 50)];
       [downloadButton setTitle:@"Download File" forState:UIControlStateNormal];
       [downloadButton addTarget:self action:@selector(downloadFileFromURL:) forControlEvents:UIControlEventTouchUpInside];
       [mainView addSubview:downloadButton];
  
       [self setRequestURL:@"http://www.mobileveda.com/r_d/mcps/optimized_av_allpdf.pdf"];
       [self.view addSubview:mainView];
  
       [super viewDidLoad];
   }
  
  - (void)viewDidUnload
  {
      [super viewDidUnload];
  }
  
  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  {
      return (interfaceOrientation == UIInterfaceOrientationPortrait);
  }
  
  
  -(void) setRequestURL:(NSString*) requestURL {
      url = requestURL;
  }
  
  -(void) downloadFileFromURL:(id) sender {
      NSURL *reqURL =  [NSURL URLWithString:url];
  
      NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:reqURL];
      NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
      if (theConnection) {
         webData = [[NSMutableData data] retain];
      } else {
          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error !" message:@"Error has occured, please verify internet connection"  delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
  
          [alert show];
          [alert release];
      }
  }
  
  -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
      [webData setLength:0]; 
  }
  
  -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
      [webData appendData:data];
  }
  
  -(void)connectionDidFinishLoading:(NSURLConnection *)connection {
     NSString *fileName = [[[NSURL URLWithString:url] path] lastPathComponent];
      NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *folder = [pathArr objectAtIndex:0];
  
      NSString *filePath = [folder stringByAppendingPathComponent:fileName];
      NSURL *fileURL = [NSURL fileURLWithPath:filePath];
      NSError *writeError = nil;
  
      [webData writeToURL: fileURL options:0 error:&amp;writeError];
      if( writeError) {
          NSLog(@" Error in writing file %@' : \n %@ ", filePath , writeError );
          return;
      }
      NSLog(@"%@",fileURL);
  }
  
  -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error !" message:@"Error has occured, please verify internet connection.."  delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
  
      [alert show];
      [alert release];
  
  }
  
  @end
  </code></pre>
  
  <p>Header File:</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  @interface ZipHandlerV1ViewController : UIViewController {
      NSMutableData *webData;
      NSString *url;
  }
  -(void) setRequestURL:(NSString*) requestURL;
  @end
  </code></pre>
  
  <p>Thank you,</p>
  
  <p><strong>Updated:</strong>
  <em>This happens because of my downloadable file was in shared hosting which having the download limitations. After I moved that file to dedicated server that working fine. and also I tried to download some other files like videos from some other sites, that also working fine.
  So, if you having problem like partial download, don't only stick with the code, check the server too</em></p>