1

したがって、私のアプリケーションでは、NSURLConnection を使用してサーバーに接続し、テキストを取得します。これは、NSURLConnection を開始する方法です。

NSMutableURLRequest * serviceRequest = [NSMutableURLRequest requestWithURL:postUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
//Some other code
[serviceRequest setValue:contentType forHTTPHeaderField:@"Content-Type"];
[serviceRequest setHTTPMethod:@"POST"];
[serviceRequest setHTTPBody:postData];

//Force to return size
[serviceRequest setValue:@"" forHTTPHeaderField:@"Accept-Encoding"];
theConnection = [[NSURLConnection alloc] initWithRequest:serviceRequest delegate:self];

現在は正常に動作していますが、このリクエストの進行状況を表すために、アプリケーションで進行状況バーを取得しようとしています。

ここに私の didReceiveResponse と didReceiveData メソッドがあります:

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    dlSize = [response expectedContentLength];
    NSLog(@"didReceiveResponse: %f", dlSize);
    [self.receivedData setLength:0]; 
}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [receivedData appendData:data];
    dlProgress = ((float) [data length] / (float) dlSize);
    NSLog(@"dlProgress: %f", dlProgress);
}

これが問題です。ご覧のとおり、そこには 2 つの NSLog がありますが、それらは要求が完了するまで呼び出されません。具体的には、サーバーに連絡して OCR 何かを行い、OCR が完了してテキストを取得すると、これら 2 つのメソッドが呼び出されます。次に、ログは次のようなものを返します。

2013-07-30 22:50:09.201 app[39381:907] didReceiveResponse: 514.000000
2013-07-30 22:50:09.202 app[39381:907] dlProgress: 1.000000
2013-07-30 22:54:39.651 app[39381:907] didReceiveResponse: 305.000000
2013-07-30 22:54:39.651 app[39381:907] dlProgress: 1.000000

リクエスト中にこれらのメソッドが呼び出されない理由はわかりませんが、このサイトの誰かが理由を知っているのではないでしょうか? とにかく、ヒント/コメント/提案は高く評価されるので、UIProgressViewをこれに接続できます。

ありがとう!

4

1 に答える 1

1

yourViewController.h NSURLResponse *response で宣言します。このメソッドでは、書き込みのみ:

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse        *)aResponse
  {

    response = aResponse;   

   }

後 (downData は NSMUtableData です)(progresso は UIProgressView です):

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

       [downData appendData:data];

       float expectedLength = [response expectedContentLength];
       float currentLength = downData.length;

       progresso.progress = currentLength / expectedLength;

      if (currentLength/expectedLength == 1) {

          //do anything
      }

      }
于 2013-07-31T08:08:24.427 に答える