2012-11-12 28 views
0

我是iOS世界的另一個新手,一直試圖弄清楚NSURLConnection如何與它的委託一起工作。我沒有多少運氣。在網上查看幾個示例之後,我創建了以下測試課程。我遇到的問題是我的委託方法都沒有被調用過。我的超時循環存在一個沒有跟蹤消息是每一個顯示。我通過tcpmon運行了這個程序,可以看到請求發送出去,響應被髮送回去,但是沒有任何東西被傳送給我的代理。NSURLConnectionDelegate沒有被調用

誰能告訴我我做錯了什麼。

謝謝。

這裏是我的代碼:

TestRequest.h 

#import <Foundation/Foundation.h> 

@interface TestRequester : NSObject <NSURLConnectionDataDelegate> 

@property BOOL finished; 

-(void)testRequest; 
@end 

和實現:

#import "TestRequester.h" 

@implementation TestRequester 

-(void)testRequest { 

    NSString *url = @"<your favourite URL>"; 
    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] 
           cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
           timeoutInterval:60.0]; 
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES]; 
    if (theConnection) { 
     NSLog(@"Connection created. Waiting...."); 
     int count = 20; 
     while (!_finished && count-- > 0) 
      [NSThread sleepForTimeInterval:0.5]; 
     if (_finished) 
      NSLog(@"Request completed"); 
     else 
      NSLog(@"Request did not complete"); 
    } else { 
     NSLog(@"Connection was not created!"); 
    } 

} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    NSLog(@"-------------> Received data"); 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    NSLog(@"-------------> Received response"); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    NSLog(@"-------------> connectionDidFinishLoading"); 
    _finished = YES; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    NSLog(@"-------------> Received error"); 
    _finished = YES; 
} 

@end 
+0

請顯示調用此類的代碼。 –

回答

0

你需要設置的連接委託作爲你的頭是類型NSURLConnectionDelegate不NSURLConnectionDataDelegate。據我所見。

@interface TestRequester : NSObject <NSURLConnectionDelegate> 

參考部分下連接協議在本頁面:) https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSURLConnection

+2

無論使用NSURLConnectionDelegate還是NSURLConnectionDataDelegate,我都會得到相同的結果。 – DaveR

0

看起來像你的連接超出範圍。你可能正在使用ARC。

TestRequest.h 

#import <Foundation/Foundation.h> 

@interface TestRequester : NSObject <NSURLConnectionDelegate> 
{ 
    NSURLConnection *theConnection; 
} 
@property BOOL finished; 

-(void)testRequest; 
@end 

-(void)testRequest { 

NSString *url = @"<your favourite URL>"; 
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] 
          cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
          timeoutInterval:60.0]; 
theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES]; 
if (theConnection) { 
    NSLog(@"Connection created. Waiting...."); 
    /*int count = 20; 
    while (!_finished && count-- > 0) 
     [NSThread sleepForTimeInterval:0.5]; 
    if (_finished) 
     NSLog(@"Request completed"); 
    else 
     NSLog(@"Request did not complete");*/ 
} else { 
    NSLog(@"Connection was not created!"); 
} 

} 
+1

連接如何超出範圍?該連接在testRequest方法中創建,直到「while」循環結束時纔會退出。 – DaveR

+0

無論如何,結果都不會改變。 – DaveR

+0

我第一次看到它時錯過了這個循環。擺脫那個循環,它阻塞了線程。我編輯了代碼。 – estobbart