Objectif C: Téléchargement du file avec la barre de progression

J'essaye de mettre une barre de progression qui se synchronise pendant le téléchargement qui se passe. Mon application peut maintenant download un file en utilisant avec ces codes …

pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithSsortingng:@"http://webaddress.com/pro/download/file.pdf"]]; NSSsortingng *resourcePDFPath = [[NSSsortingng alloc] initWithSsortingng:[[[[NSBundle mainBundle] resourcePath] ssortingngByDeletingLastPathComponent] ssortingngByAppendingPathComponent:@"Documents"]]; pdfFilePath = [resourcePDFPath ssortingngByAppendingPathComponent:@"myPDF.pdf"]; [pdfData writeToFile:pdfFilePath atomically:YES]; 

Pendant le process de ce code, l'application s'est arrêtée pendant le téléchargement, est-ce normal? Maintenant, ce que je veux, c'est mettre une barre de progression pendant ce time d'arrêt pendant le téléchargement.

J'ai essayé de regarder dans les codes que j'ai trouvés en ligne mais je suis un peu confus, je pense que j'ai besoin d'une reference étape-par-étape-bien expliquée.

En utilisant AFNetworking ,

ici le progrès est le UIProgressview

 #import <AFNetworking/AFNetworking.h>//add to the header of class -(void)downloadShowingProgress { progress.progress = 0.0; currentURL=@"http://www.selab.isti.cnr.it/ws-mate/example.pdf"; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithSsortingng:currentURL]]; AFURLConnectionOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSSsortingng *filePath = [[paths objectAtIndex:0] ssortingngByAppendingPathComponent:@"MY_FILENAME_WITH_EXTENTION.pdf"]; operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO]; [operation setDownloadProgressBlock:^(NSUInteger bytesRead, NSUInteger totalBytesRead, NSUInteger totalBytesExpectedToRead) { progress.progress = (float)totalBytesRead / totalBytesExpectedToRead; }]; [operation setCompletionBlock:^{ NSLog(@"downloadComplete!"); }]; [operation start]; } 

Utiliser NSURLConnection

 -(void)downloadWithNsurlconnection { NSURL *url = [NSURL URLWithSsortingng:currentURL]; NSURLRequest *theRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60]; receivedData = [[NSMutableData alloc] initWithLength:0]; NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES]; } - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; progress.hidden = NO; [receivedData setLength:0]; expectedBytes = [response expectedContentLength]; } - (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [receivedData appendData:data]; float progressive = (float)[receivedData length] / (float)expectedBytes; [progress setProgress:progressive]; } - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; } - (NSCachedURLResponse *) connection:(NSURLConnection *)connection willCacheResponse: (NSCachedURLResponse *)cachedResponse { return nil; } - (void) connectionDidFinishLoading:(NSURLConnection *)connection { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSSsortingng *documentsDirectory = [paths objectAtIndex:0]; NSSsortingng *pdfPath = [documentsDirectory ssortingngByAppendingPathComponent:[currentURL ssortingngByAppendingSsortingng:@".mp3"]]; NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]); [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; [receivedData writeToFile:pdfPath atomically:YES]; progress.hidden = YES; } 

Utilisez la class ASIHTTPRequest.h et ASINetworkQueue.h pour download le file.

et utilisez ce code pour la barre de progression

  request = [ASIHTTPRequest requestWithURL:@"http://webaddress.com/pro/download/file.pdf]; [request setDelegate:self]; [request setDownloadProgressDelegate:progressView]; [request setShowAccurateProgress:YES]; request.shouldContinueWhenAppEntersBackground=YES; request.allowResumeForFileDownloads=YES; [request startAsynchronous]; 

cela peut vous aider

J'ai peur que ce ne soit pas normal, utilisez une méthode asynchronous pour get le NSData.

Tout d'abord vous devriez être clair si vous voulez faire un appel synchrone ou asynchronous. Pour les applications mobiles ou toute autre application asynchronous est préféré.

Une fois que vous êtes libre, utilisez la class NSURLConnection pour extraire datatables de l'URL. Voici le bon tutoriel .

Et pour le chargement, vous pouvez commencer à progresser en démarrant la requête et l'arrêter lorsque vous recevez la connection:didFailWithError: ou connectionDidFinishLoading: méthode delegate.