Upload multiple files to iphone application (Objective c)

In my iPhone application, I want to upload several files that are in IIS with authentication. By clicking the button, I want to start the download process.

I know how to upload an authentication file.

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

and I have several delegate methods with the above code.

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

Thanks,

+4
source share
5 answers

I did this before when I wanted to load 10 XML files at the same time (it was much faster than their queues for downloading one by one). I used the libraries found here:

http://github.com/leonho/iphone-libs/tree/master

They were easy to implement, and a code example was given on the first page to get started.

 self.urls = [NSMutableArray arrayWithObjects: @"http://maps.google.com/maps/geo?output=json&q=Lai+Chi+Kok,Hong+Kong", @"http://maps.google.com/maps/geo?output=json&q=Central,Hong+Kong", @"http://maps.google.com/maps/geo?output=json&q=Wan+Chai,Hong+Kong", nil]; self.downloads = [[MultipleDownload alloc] initWithUrls: urls]; self.downloads.delegate = self; 

Good luck.

+6
source

I am not familiar with MultipleDownload , but in case it does not meet your needs, the problem that I accept is that you have one object that is a delegate to many NSURLConnections , and you want to know how to keep them straight .

All delegate methods return NSURLConnection itself as its first parameter. This way you can keep track of which data goes where the testing for which NSURLConnection calls you is. One way to do this is NSDictionary an NSDictionary , which displays a connection to its NSMutableData object. Now the trick is that you cannot make NSURLConnection key in the dictionary, because it does not match NSCopying (and you will not want it). One way around this is to use a connection address, for example:

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

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

It’s often easier for me to simply create a small wrapper object so that each object controls its own NSURLConnection , as far as I am sure MultipleDownload does, but this method is useful in a variety of cases, whether you are managing multiple table views or anything else that has a delegate .

EDIT: Replaced by% x I have higher with% p, as pointed out by Peter. He is right, and I did not think correctly. Double checking my code, I actually used% p, having encountered this error before ....

+7
source

I think the easiest way to do this is to use NSOperation - and NSOperationQueue.

This means that you can specify whether each operation will be performed sequentially or in parallel. You can even limit the number of concurrent operations - so that there are no more than 5 (say) working at a time, and then the rest of the operation queue.

This is a really great way to let the OS handle several actions - and works well with a lazy philosophy such as loading the iPhone OS.

You can then get each operation to call back when it ends, or even make progress callbacks in the main thread.

Now I changed my code to everything, working in this way, and found that it is much more reliable and user-friendly.

+2
source

I am not familiar with MultipleDownload, but if it does not meet your needs, the problem is I make sure that you have one object that is the delegate for many NSURLConnections and you want to know how to keep them straight.

All delegate methods return NSURLConnection as the first parameter. This way you can keep track of what kind of data goes by testing NSURLConnection calls you. One way to do this is an NSDictionary, which displays a connection to its NSMutableData object. The trick now is that you cannot do NSURLConnection - the key to the dictionary, because it does not match NSCopying (and you don't want this to). One way around this is to use a connection address, such as:

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

It is best to use NSValue with the constructor valueWithNonretainedObject . This way you can access the key object from NSDictionary if you need to.

+1
source

NSURLConnection is asynchronous, and init completes immediately. Just run it a few times.

 NSArray *connections = [[NSArray alloc] initWithObjects: [[NSURLConnection alloc] initWithRequest:request1 delegate:self ], [[NSURLConnection alloc] initWithRequest:request2 delegate:self ], [[NSURLConnection alloc] initWithRequest:request3 delegate:self ], [[NSURLConnection alloc] initWithRequest:request4 delegate:self ], nil]; 
0
source

Source: https://habr.com/ru/post/1286444/


All Articles