NSURLConnection - didReceiveData没有被调用 - iOS&弧

时间:2013-01-27 23:30:18

标签: ios http ios6 nsurlconnection

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

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

标题:@interface ViewController : UIViewController <NSURLConnectionDataDelegate, NSURLConnectionDelegate, NSURLConnectionDownloadDelegate, NSURLAuthenticationChallengeSender>

在身体里:

- (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!");
}

3 个答案:

答案 0 :(得分:2)

一些想法。

  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:并指示 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);
    }
    

答案 1 :(得分:0)

答案 2 :(得分:0)

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