Created
December 14, 2011 04:49
-
-
Save naokits/1475304 to your computer and use it in GitHub Desktop.
AFNetworkingを使用したPDFファイルの受信処理サンプル。PDFファイルをサーバから受信中に総受信量(MB)を表示する。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by Naoki TSUTSUI on 11/12/04. | |
// | |
/* | |
以下のコードは、実際に動作しているコードからの抜粋ですので、このままでは | |
動作しないかもしれません。fetchPdfFromServerのみ参考にしてください。 | |
インジケータ表示にMBProgressHUDを使用していますが、必要なければ該当コード | |
を削除してください。 | |
*/ | |
#import <UIKit/UIKit.h> | |
#import "MBProgressHUD.h" | |
#import "AFNetworking.h" | |
#define kFetchPDFURI @"" // PDFをダウンロードするサーバのURI | |
@interface ViewController : UIViewController <MBProgressHUDDelegate> | |
{ | |
} | |
@end | |
@implementation ViewController | |
{ | |
// プログレス表示クラス | |
__strong MBProgressHUD *_hud; | |
// ネットワークキュー | |
__strong ASINetworkQueue *_networkQueue; | |
} | |
- (void)viewWillAppear:(BOOL)animated | |
{ | |
[super viewWillAppear:animated]; | |
NSString *pdfName = @"PDFファイル名"; | |
[self fetchPdfFromServer:pdfName]; | |
} | |
- (void)showPdf:(NSString *)pathString | |
{ | |
// PDFを表示するコードを記述する | |
} | |
//----------------------------------------------------------------------------- | |
// ARC対応版 | |
// AFNetworkingを使用したPDFファイルの受信処理サンプル。 | |
// PDFファイルをサーバから受信中に総受信量(MB)を表示する。 | |
//----------------------------------------------------------------------------- | |
- (void)fetchPdfFromServer:(NSString *)pdfName | |
{ | |
// インジケータの表示開始 | |
_hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; | |
_hud.delegate = self; | |
[self.navigationController.view addSubview:_hud]; | |
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", kFetchPDFURI, pdfName]]; | |
NSURLRequest *myRequest; | |
myRequest = [NSURLRequest requestWithURL:url | |
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData | |
timeoutInterval:30.0]; | |
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:myRequest]; | |
// PDFの保存場所を指定 | |
NSString *documentDir; | |
documentDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; | |
NSString *downloadDestinationPath = [documentDir stringByAppendingPathComponent:pdfName]; | |
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:downloadDestinationPath append:NO]; | |
// ダウンロード中にデータを受信するごとに実行される | |
[operation setDownloadProgressBlock:^(NSInteger byteRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead) { | |
// サーバがサイズを返さない場合は、totalBytesExpectedToReadに-1が返る。 | |
NSString *result = [NSString stringWithFormat:@"%0.2f MB", totalBytesRead / 1024.0f / 1000.0f]; | |
_hud.labelText = result; | |
}]; | |
// ダウンロード終了後に実行される | |
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { | |
NSLog(@"ダウンロード成功! PDFの保存先:%@", downloadDestinationPath); | |
// インジケータの表示終了 | |
[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES]; | |
// ダウンロードしたPDFを表示 | |
[self showPdf:downloadDestinationPath]; | |
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
NSLog(@"Error: %@", [error localizedDescription]); | |
}]; | |
[operation start]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment