-1

I'm starting a NSURLConnection and I need to save data received from internet. In the - (void)connectionDidFinishLoading:(NSURLConnection *)connection I need to save data with different name using the original url as name of the data... How Can get this info (url) in the connectionDidFinishLoading using async request? If it is not possible can suggest me some other ways to do what I asked? Thanks Paolo

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
paul_1991
  • 245
  • 4
  • 13

2 Answers2

1

*NOW ASIHTTPRequest Library is No Longer supported by the author so its good to start adopting some other libraries *

I would suggest you use ASIHTTP request. I have been using this for a long time. The below code sample is for downloading data from a url asynchronously.

- (IBAction)grabURLInBackground:(id)sender
{
   NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setDelegate:self];
   [request startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
   // Use when fetching text data
   NSString *responseString = [request responseString];

   // Use when fetching binary data
   NSData *responseData = [request responseData];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
   NSError *error = [request error];
}

UPDATE:

- (IBAction)grabURLInBackground:(id)sender
{
   NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
   __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setCompletionBlock:^{
      // Use when fetching text data
      NSString *responseString = [request responseString];

      // Use when fetching binary data
      NSData *responseData = [request responseData];

      //here you have access to NSURL variable url.
   }];
   [request setFailedBlock:^{
      NSError *error = [request error];
   }];
   [request startAsynchronous];
}

Try using GCD in ASIHTTP. Here inside the block you'll have access to the variable url.

fftoolbar
  • 175
  • 1
  • 1
  • 11
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
1

** Answer valid only pre-iOS5. Since iOS5 Apple introduced the -originalRequest method that allows to avoid any further subclassing for this special purpose. In general Apple introduced the the NSURLConnection class so many improvements that it makes no longer necessary to subclass NSURLConnection unless non-trivial behaviours are required ** You can subclass NSURLConnection by adding an extra property called

NSURL originalURL
and then start it. When the delegate finish method is executed you can retrieve this property and do the rest of the job. *

E.g. (I will show relevant parts, don't copy and paste please):

MyURLConnection.h

@interface MyURLConnection:NSURLConnection { @property (nonatomic,retain) NSURL *originalURL; } @end MyURLConnection.m

@implementation MyURLConnection @synthesize originalURL; In your calling class:

MyURLConnection *myConnection = [[MyURLConnection alloc] initWithRequest:myRequest delegate:myDelegate]; myConnection.originalURL = [request URL]; [myConnection start]; and finally in the delegate: - (void)connectionDidFinishLoading:(NSURLConnection *)connection { MyURLConnection *myConn = (MyURLConnection)connection; NSURL *myURL = myConn.originalUrl; // following code }
viggio24
  • 12,316
  • 5
  • 41
  • 34
  • NSURLConnection already has an instance method -originalRequest so subclassing to add originalURL completely unneeded. myConnection.originalRequest.URL is the way to go. – Gallonallen Aug 23 '13 at 19:37
  • Yes, this method has been introduced with ios5, the answer is probably dated during the transition phase from ios4 to ios5. I have added the clarification in the answer. – viggio24 Aug 24 '13 at 03:05