2

サーバーに接続してそこからデータをダウンロードするプロジェクトに取り組んでいます。接続が中断された場合のダウンロードの再開をサポートしたい。私のアプローチは、ダウンロードしたデータの部分を宛先ファイルに保存することです。接続が中断された場合は、connection:didWriteData:totalBytesWritten:expectedTotalBytes を使用してダウンロードした部分をマークし、後で停止した部分からサーバーで再開します。

私のコード:

- (IBAction)connectToServer:(UIButton *)sender
{
    // setup url and send request to server
    NSURL *url = [NSURL URLWithString:BASED_URL];
    self.urlRequest = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    self.urlConnection = [[NSURLConnection alloc] initWithRequest:self.urlRequest delegate:self];

    // start receive data if connection established
    if (self.urlConnection){
    self.receivedData = [NSMutableData data];
    NSLog(@"starting to receive data");

} else {
    // handle error
    NSLog(@"failed to connect to server");
    }
}

- (void)doSomethingWithData
{
    // handle data here
}


#pragma NSURLConnectionDataDelegate

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse     *)response
{
    [self.receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // received data
    [self.receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    // error connection
    NSLog(@"connection failed");
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Data receiving succeed, received: %d bytes of data", [self.receivedData length]);

}

- (void)connection:(NSURLConnection *)connection didWriteData:(long long)bytesWritten totalBytesWritten:(long long)totalBytesWritten expectedTotalBytes:(long long)expectedTotalBytes
{
    NSLog(@"not getting called");
}

私の質問は、「connection:didWriteData:totalBytesWritten:expectedTotalBytes」メソッドが呼び出されないのはなぜですか?

本当にありがとう!クリス

4

2 に答える 2

1

これを.hファイルに追加しましたか:

 @interface yourViewContoller : UIViewController <NSURLConnectionDataDelegate, NSURLConnectionDelegate>
于 2012-09-24T06:00:14.697 に答える