NSURLConnection – didReceiveData没有被调用 – iOS和ARC

我正在尝试使用twitter流API,我正在尝试使用NSURLConnection获取流。 由于它不适用于Twitter,我简化了一切,并试图从谷歌的网站上获取一些源代码,但这不起作用。

连接开始和结束,但不调用didReceiveData委托。 我确定我错过了什么。 希望你的家伙可以帮助我!

在标题中: @interface ViewController : UIViewController

并在身体:

 - (void)viewDidLoad { [super viewDidLoad]; NSURLConnection *connection; NSMutableURLRequest *request; // Do any additional setup after loading the view, typically from a nib. request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com"]]; [request setHTTPMethod:@"GET"]; connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; [connection start]; } - (void)connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL { NSLog(@"Stream finished."); } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@", dataString); } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ NSLog(@"Connection failed!"); } 

几个想法。

  1. 您的connectionDidFinishLoading声明看起来不正确。 标准NSURLConnectionDataDelegate方法没有destinationURL参数:

     - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"%s", __FUNCTION__); } 
  2. 鉴于存在NSURLAuthenticationChallengeSender ,如果您期待挑战(Google网站无法获得),那么您显然会相应地处理它:

     - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { // For example, if you have a userid/password to use, see if this is the first // challenge, and then tell NSURLConnection to try using those credentials, and if // it failed a second time, you might just cancel the authentication challenge. if (challenge.previousFailureCount == 0) { NSURLCredential *credential = [NSURLCredential credentialWithUser:kUserID password:kPassword persistence:NSURLCredentialPersistenceForSession]; [challenge.sender useCredential:credential forAuthenticationChallenge:challenge]; } else { [challenge.sender cancelAuthenticationChallenge:challenge]; } } 
  3. 顺便说一下,当您使用initWithRequestconnectionWithRequest时,您不希望调用start方法。 只有在执行简单的initWithRequest:delegate:startImmediately:时才需要它initWithRequest:delegate:startImmediately:并指示它不能 startImmediately

  4. 无论如何,我使用以下,它工作正常:

     - (void)viewDidLoad { [super viewDidLoad]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.google.com"]]; [NSURLConnection connectionWithRequest:request delegate:self]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"%s", __FUNCTION__); } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%s: %@", __FUNCTION__, dataString); } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ NSLog(@"%s error=%@", __FUNCTION__, error); } 

有人说这是他们的SOAP格式NSURLConnection委托方法:didReceiveData没有调用…为什么? (iPhone SDK)

这也可能是你正在寻找的NSURLConnection didReceiveData没有被调用

您的NSURLConnection本地变量connection超出了viewDidLoad末尾的范围。 向ViewController添加属性以将NSURLConnection变量保存在范围内。