iphone download several files
In my app i need to download several plist.
to download a plist i 开发者_开发知识库use the NSURLconnection
in my code i use an UIAlertView
with a UIActivityIndicator
then when the download is finished i add a button to the alert to dismiss it.
To download the plist i use somewhere in my code an NSURL
set to the adresse where the plist is, next i set a NSURLRequest
with the url cache policy and a timeout interval.
Then i set my NSMutableData
to the NSURL
connection with a NSURLRequest
.
In the delegate didReceiveData
: i append data to my mutable data object, in the didFailWithError:
i handle error. And finaly in the connectionDidFinishLoading
i serialize my data to a plist so i can write to file my plist, and release my alertview.
My problem is : how can i do if i have sevetal file to download because the connectionDidFinishLoading
is called each time my NSURLConnection
is finished but i want to release my UiAlert
when everything is finished. But when the first plist is downloaded my code in the connectionDidFinishLoading
will fire.
here is my code :
in the view did load :
// set the UiAlert in the view did load
NSURL *theUrl = [NSURL URLWithString:@"http://adress.com/plist/myPlist.plist"];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:theUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
self.plistConnection = [[ NSURLConnection alloc] initwithRequest:theRequest delegate:self startImmediatly:YES];
//plistConnection is a NSURLConnection
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[incomingPListData appendData:data];
}
-(void)connection:(NSURLConnection *)connectionDidFailWithError:(NSError *)error {
// handle error here
}
-(void)connectionDidFinisloading:(NSURLConnection *) connection {
NSPropertyListFormat format;
NSString *serialErrorString;
NSData *plist = [NSPropertyListSerialisation propertyListFromData:incomingPlistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&serialErrorString];
if (serialErrorString) {//error}
else { // create path and write plist to path}
// change message and title of the alert
so if i want todownload an another file where do i put the request the connection and how can i tell the didFinishLoading
to fire code when all my file are downloaded.
thanks to all
You can iterate over an array of resources you wish to download, and allocate a request for each of them. It is possible to set a tag to a connection (e.g. the index of the URL in the array) which you can evaluate in the connectionDidFinishLoading. If you hold the information, which requests are sent and which are finished, you can easily see, if all files have been loaded.
I think unset provided a good answer. I understand that you don't get it (you will someday), as I remember myself being new to programming and such.
I therefore provide another, much simpler option to evaluate if all downloads did finish.
you simply use a counter that you define in your .h file,
int activeDownloads;
in your implementation (.m) File, wherever you start all your downloads, set activeDownloads
to zero before any of your downloads start
activeDownloads = 0;
before you start a download you increase the number of activeDownloads
++activeDownloads;
if a download finishes or fails you decrease the same countervariable
- (void)connectionDidFinishLoading:(NSURLConnection *)connection { --activeDownloads;}
(i didn't write down the method that gets called if a download fails...
also everytime a connection finishes or fails you have to check if the one that finished or railed was the last one.
you do that by simply checking if activeDownloads
is equal to zero. If that is the case, you can add the "close" Button to your AlertView.
The drawback of this solution is, that you're unable to track which connection succeeded and which failed. (well, you are, but activeDownloads
don't help you with that)
hope i could help
cheers
精彩评论