2010-07-01 5 views
1

Me again! J'utilise NSURL pour obtenir un fichier puis l'analyser. Je cherche depuis quelques heures sur une barre de progression que j'essaie de mettre en œuvre dans mon application. Je sais que je dois d'abord obtenir la taille du fichier, puis continuer à mettre à jour combien de données j'ai téléchargé comme je continue à tirer. J'ai vu l'exemple en utilisant "ASIHTTPRequest" mais y at-il un moyen de le faire avec ce que j'ai déjà?Obtenir la taille du fichier URL avant l'analyse

C'est ici que je lance le téléchargement. Est-ce que quelqu'un peut me diriger dans la bonne direction sur la façon d'obtenir la taille du fichier, puis comment mettre à jour combien j'ai téléchargé. Merci d'avance!

Répondre

1

Vous devez utiliser NSURLConnection si vous souhaitez obtenir la taille et la progression du fichier. Vous obtenez des méthodes déléguées que vous pouvez utiliser pour surveiller la progression. La méthode didSendBodyData: delegate vous indique la quantité de données en octets. Le connectionDidFinishLoading est l'endroit où vous utilisez receiveData à utiliser dans votre code NSXMLParser.

NSURLRequest *theRequest = [NSURLRequest requestWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0]; 
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
if (theConnection) { 

    receivedData = [[NSMutableData data] retain]; 
} 

} 
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 
// This method is called when the server has determined that it 
// has enough information to create the NSURLResponse. 

// It can be called multiple times, for example in the case of a 
// redirect, so each time we reset the data. 

// receivedData is an instance variable declared elsewhere. 
[receivedData setLength:0]; 
} 
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ 
// release the connection, and the data object 

// inform the user 
NSLog(@"Connection failed! Error - %@ %@", 
     [error localizedDescription], 
     [[error userInfo] objectForKey:NSErrorFailingURLStringKey]); 
[connection release]; 
// receivedData is declared as a method instance elsewhere 
[receivedData release]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 
// Append the new data to receivedData. 
// receivedData is an instance variable declared elsewhere. 
[receivedData appendData:data]; 
} 
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite{ 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{ 
+0

Merci pour la suggestion. Je vais devoir refaire du code mais vous m'avez orienté dans la bonne direction. Récupérez les données, puis analysez ... au lieu d'analyser les données! GOT IT! :) – Louie

4
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [webData setLength: 0]; 
    CGFloat size = [[NSString stringWithFormat:@"%lli",[response expectedContentLength]] floatValue]; 
    NSLog(@"Size : %f",size); 

} 

code ci-dessus vous donnera la taille totale

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 


    [webData appendData:data]; 
    totalDownloadedData += [data length]; // global integer 
    NSLog(@"Status : %d/%f",totalDownloadedData,size); 

} 

code ci-dessus vous montre l'état actuel du téléchargement

+0

Solution fantastique ......... –

Questions connexes