title: Know the download progress for update an UIprogressView
body: <p>my app downloads a video from internet and saves it in the iPhone.</p>
  
  <p>I'm using this code</p>
  
  <pre><code>NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
  
  [request setHTTPMethod:@"GET"];
  NSError *error;
  NSURLResponse *response;
  
  
  NSString *documentFolderPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  NSFileManager *fileManager = [NSFileManager defaultManager];
  NSString *videosFolderPath = [documentFolderPath stringByAppendingPathComponent:@"videos"]; 
  
  //Check if the videos folder already exists, if not, create it!!!
  BOOL isDir;
  if (([fileManager fileExistsAtPath:videosFolderPath isDirectory:&amp;isDir] &amp;&amp; isDir) == FALSE) {
      [[NSFileManager defaultManager] createDirectoryAtPath:videosFolderPath withIntermediateDirectories:YES attributes:nil error:nil];
  }
  
  NSString *filePath = [videosFolderPath stringByAppendingPathComponent:@"name.mp4"];
  
  if ([fileManager fileExistsAtPath:filePath ] == YES) {
      NSLog (@"File exists");
  }
  else {
      NSLog (@"File not found");
      NSData *urlData;
      NSString *downloadPath = @"http://www.mywebsite.com/name.mp4";
      [request setURL:[NSURL URLWithString:downloadPath]];
      urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&amp;response error:&amp;error];
  
      BOOL written = [urlData writeToFile:filePath atomically:NO];
      if (written)
          NSLog(@"Saved to file: %@", filePath);
      else {NSLog(@"problem");}
  }
  </code></pre>
  
  <p>All works fine, but I want to add a progress View, to indicate the status of the download.</p>
  
  <p>The problem (I think but I'm not sure) is that my NSURLConnection hasn't itself as delegate, so methods like</p>
  
  <pre><code>- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)data 
  </code></pre>
  
  <p>or</p>
  
  <pre><code>-(void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite{
  </code></pre>
  
  <p>are never called.</p>
  
  <p>I tried to use </p>
  
  <pre><code>urlData = [[NSURLConnection alloc] initWithRequest:request delegate:self];
  </code></pre>
  
  <p>but the app crashes when I try to write the file</p>
  
  <pre><code>[urlData writeToFile:filePath atomically:NO];
  </code></pre>
  
  <p>What can I do?
  Thanks</p>