2010-01-23 50 views
5

我很难找到NSURLConnection委托方法实现的任何示例。来自苹果的SeismicXML示例不完整。例如,他们不纳入是否有使用所有NSURLConnection委托方法的完整示例?

-connection:willSendRequest:redirectResponse: 

也许有一个很好的文字。我已经通过所有关于此的Apple资料。

+1

为什么你想看到所有的委托方法的例子吗?如果你问一个更具体的问题,那么人们可能会为你提供更好的答案。 – 2010-01-24 04:29:40

回答

17

下面是一个实现我一直与最近:

.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

谢谢,但不完整。我发现没有人充分利用所有的委托方法。 – openfrog 2010-01-23 19:58:21

+0

啊,对不起,是的,我没有执行所有......好奇地看到答案的其余部分。 – 2010-01-23 20:06:48

+0

负载中的NSURLConnection泄漏。 – titaniumdecoy 2010-10-11 21:49:16

相关问题