2010-01-23 49 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

相關問題