【问题标题】:NSURLConnection connection:didReceiveData: is not called on ios5NSURLConnection 连接:didReceiveData:不在 ios5 上调用
【发布时间】:2012-10-16 14:32:24
【问题描述】:

一个奇怪的问题。 我想从网上加载一张图片,所以我使用 NSURLConnection 来做。 当我在 ios4.3 上测试我的代码时,一切正常。 但是当我在 ios5.0 上启动我的应用程序时,我发现连接:didreceiveData 不管我做什么都没有被调用。 otherelse函数调用正常,就像ios4.3的connectionDidFinishLoading和ios5.0的connectionDidFinishDownloading一样。 所以你们,谁能帮助我,谢谢!

-(void)load
{
    if(isDownloading){
        return;
    }
    if(conn != nil){
        [conn release];
    }
    if(data != nil){
        [data release];
        data = nil;
    }
    [self isDownloading:YES];
    ImageDownloadData* imageDownloadData = [imageList objectAtIndex:count];
    NSURL* url = [imageDownloadData url];
    NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if(conn){
        [conn start];
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)rd
{
    NSLog(@"data");
    if(!data){
        data = [[NSMutableData alloc] initWithData:rd];
        return;
    }
    [data appendData:rd];
}

【问题讨论】:

    标签: ios5 nsurlconnection


    【解决方案1】:

    我无法确定这是否与您遇到的问题相同,但我遇到了类似的问题,并通过取出 in 方法和对 NSURLConnectionDownloadDelegate 的引用来解决它。显然NSURLConnection 的代表一次只能实现从NSURLConnectionDelegate 派生的两个协议之一。

    There's been some odd API changes between 4.3 and 5.0。 Apple 将 NSURLConnectionDelegate 从非正式协议更改为正式协议,并将其中的一些方法扩展为两个额外的子协议:NSURLConnectionDataDelegateNSURLConnectionDownloadDelegate。 (奇怪的是,他们贬低了 NSURLConnectionDelegate 中相同的方法,但没有记录他们移动到哪里。)

    在针对 6.0 API 编译我的代码时,我注意到如果我从 NSURLConnectionDataDelegateNSURLConnectionDownloadDelegate 实现方法,我无法让 Cocoa Touch 调用 connection: didReceiveData:。我实现的所有其他方法都按预期调用。

    【讨论】:

      猜你喜欢
      • 2012-06-11
      • 1970-01-01
      • 1970-01-01
      • 2011-12-09
      • 2014-07-21
      • 1970-01-01
      • 2011-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多