Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 800059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:13:54+00:00 2026-05-14T23:13:54+00:00

I would like to download files directly from an URL to the disk using

  • 0

I would like to download files directly from an URL to the disk using objective-c on the iPhone os.

Currently I am using NSURLConnection to send a synchronousRequest, writing the returned NSData into a file.

How can I change the download handling (still having the request beeing synchronous, it is already in a background thread) to write the data directly to disk, not using memory variables to store the complete content (only small parts)?

A sample code would be appreciated.

Thank you all in advance for your responses!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:13:55+00:00Added an answer on May 14, 2026 at 11:13 pm

    You can do this, but it’s a bit complicated to set up. Here’s how I’d do it:

    warning: the following code was typed in a browser and compiled in my head. Also, there’s not a lot of error handling. Caveat Implementor.

    //NSURLConnection+DirectDownload.h
    @interface NSURLConnection (DirectDownload)
    
    + (BOOL) downloadItemAtURL:(NSURL *)url toFile:(NSString *)localPath error:(NSError **)error;
    
    @end
    
    //NSURLConnection+DirectDownload.m
    @implementation NSURLConnection (DirectDownload)
    
    + (BOOL) downloadItemAtURL:(NSURL *)url toFile:(NSString *)localPath error:(NSError **)error {
      NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url];
      //configure the request, or leave it as-is
    
      DirectDownloadDelegate * delegate = [[DirectDownloadDelegate alloc] initWithFilePath:localPath];
      NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
      [delegate autorelease];
      [request release];
    
      while ([delegate isDone] == NO) {
        [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
      }
    
      [connection release];
    
      NSError * downloadError = [delegate error];
      if (downloadError != nil) {
        if (error != nil) { *error = [[downloadError retain] autorelease]; }
        return NO;
      }
    
      return YES;
    }
    
    //DirectDownloadDelegate.h
    @interface DirectDownloadDelegate : NSObject {
      NSError *error;
      NSURLResponse * response;
      BOOL done;
      NSFileHandle * outputHandle;
    }
    @property (readonly, getter=isDone) BOOL done;
    @property (readonly) NSError *error;
    @property (readonly) NSURLResponse * response;
    
    @end
    
    //DirectDownloadDelegate.m
    @implementation DirectDownloadDelegate
    @synthesize error, request, done;
    
    - (id) initWithFilePath:(NSString *)path {
      if (self = [super init]) {
        if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
          [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
        }
        [[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];
        outputHandle = [[NSFileHandle fileHandleForWritingAtPath:path] retain];
      }
      return self;
    }
    
    - (void) dealloc {
      [error release];
      [response release];
      [outputHandle closeFile];
      [outputHandle release];
      [super dealloc];
    }
    
    - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)anError {
      error = [anError retain];
      [self connectionDidFinishLoading:connection];
    }
    
    - (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)someData {
      [outputHandle writeData:someData];
    }
    
    - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse {
      response = [aResponse retain];
    }
    
    - (void) connectionDidFinishLoading:(NSURLConnection *)connection {
      done = YES;
    }
    

    The basic idea is that you create a standard NSURLConnection, which is normally asynchronous, but just block the thread by spinning the runloop yourself until the connection is done. You also use a custom url connection delegate to just pipe any data the connection receives directly to a file.

    You can now do:

    NSError * downloadError = nil;
    BOOL ok = [NSURLConnection downloadItemAtURL:someURL toFile:someFile error:&downloadError];
    if (!ok) {
      NSLog(@"ack there was an error: %@", error);
    } else {
      NSLog(@"file downloaded to: %@", someFile);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my app i would like to download images and songs from server url
I would like to download files from the web to the internal storage of
I would like to download an mp3 file from some site, save it to
I would like to implement the download estimator using the JavaScript and the Ajax.
I would like ask if there's a way to download an android layout from
Some background information The files I would like to download is kept at the
I would like to download a file directly into my working directory I can
I am trying to download files using NSURLConnection and show the download progress. But
I would like to download the files that are the difference between two branches
I would like to download parts of files on a FTP server. I've got

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.