Search code examples
iphoneobjective-cnsurlconnectionnsoperationqueue

NSURLConnection. I need a help saving data received


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


Solution

  • ** 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 }