2013-05-09 11 views
17

ダウンロード中に進行中の進行状況バーを表示しようとしています。 私のアプリは今、それが正常で、アプリがダウンロード中に停止し、このコードの処理中...目的C:プログレスバーでファイルをダウンロードする

pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://webaddress.com/pro/download/file.pdf"]]; 

    NSString *resourcePDFPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]]; 

    pdfFilePath = [resourcePDFPath stringByAppendingPathComponent:@"myPDF.pdf"]; 

    [pdfData writeToFile:pdfFilePath atomically:YES]; 

をこのコードで使用してファイルをダウンロードできますか? 私が望むのは、ダウンロード中にその停止時間中にプログレスバーを置くことです。

オンラインで見つかったコードを調べてみましたが、ちょっと混乱しています。ステップバイステップの説明が必要です。

答えて

59

ここ進歩はNSURLConnection

-(void)downloadWithNsurlconnection 
{ 

    NSURL *url = [NSURL URLWithString: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); 

    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:[currentURL stringByAppendingString:@".mp3"]]; 
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]); 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
    [receivedData writeToFile:pdfPath atomically:YES]; 
    progress.hidden = YES; 
} 
+0

AFURLConnectionOperationは宣言されていないIDです – SeongHo

+0

#import クラスのヘッダ –

+0

試してみましたが見つかりませんでした。私はどんなフレームワークを使うべきですか? – SeongHo

0

NSDataを取得するには、非同期メソッドを使用するのが普通です。

1

ファイルのダウンロードには、ASIHTTPRequest.hクラスとASINetworkQueue.hを使用します。

、これはあなたが同期呼び出しまたは非同期を行うかどうかを明確にする必要があり、すべての

+0

を使用してUIProgressview

#import <AFNetworking/AFNetworking.h>//add to the header of class -(void)downloadShowingProgress { progress.progress = 0.0; [email protected]"http://www.selab.isti.cnr.it/ws-mate/example.pdf"; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:currentURL]]; AFURLConnectionOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"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]; } 

である私は、 "ダウンロード" のボタンクリックでこれを置く必要がありますか? – SeongHo

+0

はい、このコードをボタンクリック機能に配置したいのですが... – Nithinbemitk

0

ファーストを助けるかもしれない

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]; 

プログレスバーのために、このコードを使用します。 モバイルアプリやその他のアプリの場合は、非同期が優先されます。

クリアしたら、NSURLConnectionクラスを使用してURLからデータを取得します。 ここにはgood tutorialがあります。

ロード中は、要求を開始するときに進捗を開始し、connection:didFailWithError:またはconnectionDidFinishLoading:デリゲートメソッドを受け取ったときに処理を中止できます。 AFNetworkingを使用して

+0

swift @Inder Kumar RathoreのNSURLConnectionを使用してPDFファイルを同期してダウンロードするには –

関連する問題