开发者

Are there complete examples that make use of all the NSURLConnection delegate methods?

I have a hard time to find any examples for NSURLConnection delegate method implemenetations. The SeismicXML example from apple is incomplete. For in开发者_开发问答stance, they don't incorporate

-connection:willSendRequest:redirectResponse:

Maybe there's a good text out there. I went already through all the Apple material regarding this.


Here's an implementation I've been working with lately:

.h:
    NSMutableData *responseData;

.m:
    - (void)load {
        NSURL *myURL = [NSURL URLWithString:@""];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL
                                                 cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                             timeoutInterval:60];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    responseData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [responseData release];
    [connection release];
    [textView setString:@"Unable to fetch data"];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    NSLog(@"Succeeded! Received %d bytes of data",[responseData
                                                   length]);
    NSString *txt = [[[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding] autorelease];

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜