NSURLConnection connection:didReceiveData: is not called on ios5

淺唱寂寞╮ 提交于 2019-12-10 11:28:55

问题


A weird problem. I wanna load an image from web, so i use NSURLConnection to do it. Everything is ok when i do testing my code on ios4.3. But when i'm launch my app on ios5.0, i found the connection:didreceiveData haven't been called whatever what i did. otherelse functions is called normally, just like connectionDidFinishLoading in ios4.3 and connectionDidFinishDownloading in ios5.0. so u guys, who can help me, thanks advanced!

-(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];
}

回答1:


I can't be sure if this is the same problem you're having, but I had a similar issue, and resolved it by taking out the in methods and references to NSURLConnectionDownloadDelegate. Apparently delegates of NSURLConnection can only implement one of the two protocols that are derived from NSURLConnectionDelegate at a time.

There's been some odd API changes between 4.3 and 5.0. Apple changed the NSURLConnectionDelegate from an informal protocol to a formal one, and branched out some of the methods into two additional subprotocols: NSURLConnectionDataDelegate and NSURLConnectionDownloadDelegate. (Oddly though, they depreciated the identical methods in NSURLConnectionDelegate but didn't document where they moved to.)

I've been noticing when compiling my code against the 6.0 API that I've been having trouble getting Cocoa Touch to call connection: didReceiveData: if I Implement methods from both NSURLConnectionDataDelegate and NSURLConnectionDownloadDelegate. All the other methods I implemented were called as expected.



来源:https://stackoverflow.com/questions/13085007/nsurlconnection-connectiondidreceivedata-is-not-called-on-ios5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!