Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 未调用NSURLConnectionDownloadDelegate方法_Iphone_Ios_Xcode_Nsurlconnection - Fatal编程技术网

Iphone 未调用NSURLConnectionDownloadDelegate方法

Iphone 未调用NSURLConnectionDownloadDelegate方法,iphone,ios,xcode,nsurlconnection,Iphone,Ios,Xcode,Nsurlconnection,我正在做一个连接到服务器并从中下载数据的项目。我喜欢在连接中断时支持恢复下载。我的方法是将下载的部分数据保存到目标文件中;如果连接中断,我想使用connection:didWriteData:TotalBytesWrite:expectedTotalBytes标记下载的部分,然后使用服务器从停止的部分恢复 我的代码: - (IBAction)connectToServer:(UIButton *)sender { // setup url and send request to serv

我正在做一个连接到服务器并从中下载数据的项目。我喜欢在连接中断时支持恢复下载。我的方法是将下载的部分数据保存到目标文件中;如果连接中断,我想使用connection:didWriteData:TotalBytesWrite: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:TotalBytesWrite:expectedTotalBytes”方法从未被调用

非常感谢!
chris

您是否在.h文件中添加了此项:

 @interface yourViewContoller : UIViewController <NSURLConnectionDataDelegate, NSURLConnectionDelegate>
@界面YourViewController:UIViewController
根据:

NSURLConnectionDownloadDelegate协议描述了应该由使用报刊亭工具包的downloadWithDelegate:方法创建的NSURLConnection实例的委托实现的方法

此外,它还说:

如果您直接使用NSURLConnection,那么您的委托类应该实现NSURLConnectionDataDelegate协议中定义的方法