是否有完整的示例使用所有NSURLConnection委托方法? [英] Are there complete examples that make use of all the NSURLConnection delegate methods?

查看:76
本文介绍了是否有完整的示例使用所有NSURLConnection委托方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难找到NSURLConnection委托方法实现的任何示例。 Apple的SeismicXML示例不完整。例如,它们不包含

I have a hard time to find any examples for NSURLConnection delegate method implemenetations. The SeismicXML example from apple is incomplete. For instance, they don't incorporate

-connection:willSendRequest:redirectResponse:

也许那里有好的文字。我已经完成了关于此的所有Apple资料。

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

}

这篇关于是否有完整的示例使用所有NSURLConnection委托方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆