3

Ios を学習するために、いくつかの例をダウンロードしました。

例は次のとおりです。

  • AFネットワーキング;
  • アプリ内購入 (RayW から);
  • MBProgressHud;

ビューコントローラーで、Inapp 購入シングルトンの例をトリガーする UIbutton を押し、AFHTTPRequestOperation を使用してサーバーからファイルのダウンロードを開始します。コミュニケーションのこの部分が機能します。しかし、私が達成したいのは、ダウンロード中にhudを更新することです。ファイルが10Mbを超えるため。

それで、問題は、ダウンロードの進行状況でhudを更新するにはどうすればよいですか? 描き下ろしてみます。

  • Viewcontroller でボタンを押すと、hud が表示されます。

    • --> リクエストは、ネットワーキング部分を処理する Singleton InApp ヘルパー クラスに送信されます。

      • --> その後、ファイルのダウンロードのために AFHTTPRequestOperation がシングルトン クラス内で呼び出されます。

        • ---> このダウンロード中に、進行状況に setDownloadProgressBlock メソッドを使用します。

しかし、viewcontroller の hud に進捗情報を送信するにはどうすればよいですか?

ありがとう。

4

2 に答える 2

3

これは、@matttのアドバイスに従って、同様の問題で私がしたことです。私の Singleton InApp ヘルパーにはivarproductDownloadURLと、呼び出し元prepareForDownloadに an を返すメソッドがあります。AFHTTPRequestOperation

- (AFHTTPRequestOperation * )prepareForDownload:(NSString *)productIdentifier
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_productDownloadURL]];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:productIdentifier];
    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

    return  operation;
}

私のRootViewControllerは、次のように/ /ブロックを使用AFHTTPRequestOperationして設定し、リクエストを作成します。downloadProgresssuccessfailure

AFHTTPRequestOperation *operation = [[InAppRageIAPHelper sharedHelper] prepareForDownload:productIdentifier];
[operation setDownloadProgressBlock:^(NSInteger bytesRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead) {
     float percentDone = ((float)((int)totalBytesRead) / (float)((int)totalBytesExpectedToRead));    
     [(UIProgressView *)_hud.customView setProgress:percentDone];
     _hud.labelText = [NSString stringWithFormat:@"%f",percentDone];
 }];

 [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
   _hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"success.png"]];
   [self performSelector:@selector(dismissHUD:) withObject:nil afterDelay:1.5];
 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
   _hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error.png"]];
 }];
 [operation start];

hudMBProgressHUDです。モードを使用して進行状況の表示を強化することもできMBProgressHUDModeDeterminateます。

于 2012-03-12T17:40:08.347 に答える
2

コントローラーからリクエストを作成し、変数を作成してキューに入れるときに操作への参照を保持します (これを行うには、 を使用して中間操作オブジェクトを作成しHTTPOperationWithRequest:success:failure、手動でenqueueHTTPOperation:.

の本体で、進行状況ビューsetDownloadProgressBlockのプロパティを設定します ( 0.0 と 1.0 の間で正規化するには、 で割るprogress必要があります。bytesReceivedbytesExpectedToReceive

于 2012-02-20T20:47:11.770 に答える