Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading multiple files in iphone app(Objective c)

In my iPhone app I want to download multiple files which are on IIS with authentication. On a button click i want to start the downloading process.

I know how to download a file with authentication.

    NSURLRequest* request =
               [NSURLRequest requestWithURL:mMovieURL 
                             cachePolicy:NSURLRequestUseProtocolCachePolicy 
                             timeoutInterval:60.0];
    movieConnection =
            [[NSURLConnection alloc] initWithRequest:request delegate:self ];

and i have couple of delegate methods with the above code.

But how to do it with mutliple downlaods going at the same time.

Thanks,

like image 326
nbojja Avatar asked Dec 06 '22 05:12

nbojja


1 Answers

I'm not familiar with MultipleDownload, but in case it doesn't meet your needs, the issue I take it is that you have a single object that is the delegate to many NSURLConnections, and you want to know how to keep them straight.

The delegate methods all return the NSURLConnection itself as their first parameter. So you can keep track of which data goes where by testing which NSURLConnection is calling you back. One way to do this is with an NSDictionary that maps the connection to its NSMutableData object. Now the trick is that you can't make an NSURLConnection be the key in a dictionary because it doesn't conform to NSCopying (and you wouldn't want it to). One way to work around this is to use the address of the connection such as:

NSString *key = [NSString stringWithFormat:@"%p", connection];

This will return a unique key for any object (the hex representation of its address). Some people use description for this purpose, but I don't like that because it's not a well defined interface. There's no promise that it be unique. In systems where I do this a lot, I implement the above -stringWithFormat: in a method called -uniqueIdentifier and make it a category on NSObject so anything can be tracked in a dictionary.

I often find it's easier just to create a small wrapper object so that each object controls its own NSURLConnection, much as I'm sure MultipleDownload is doing, but still this technique is useful in a variety of cases, whether you're managing multiple tableviews, or anything else that has a delegate.

EDIT: Replaced %x I had above with %p as noted by Peter. He's right, and I wasn't thinking correctly. Double-checking my code, I actually have been using %p, having run into this error before....

like image 97
Rob Napier Avatar answered Dec 26 '22 11:12

Rob Napier