NSURLConnection - didReceiveData没有被调用 - iOS&弧 [英] NSURLConnection - didReceiveData didn't get called - iOS & ARC

查看:489
本文介绍了NSURLConnection - didReceiveData没有被调用 - iOS&弧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently experimenting with the twitter streaming api and i'm trying to get a stream with NSURLConnection. As it doesn't work with twitter, i simplified everything and tried to get some source-code out of google's website, but this doesn't work neither.

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

The connection starts and ends, but without calling the didReceiveData delegate. I'm sure i'm missing something. Hope you guy's can help me!

在标题中: @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!");
}


推荐答案

一些想法。


  1. 您的声明 connectionDidFinishLoading doesn'好看。标准 NSURLConnectionDataDelegate 方法没有 destinationURL 参数:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"%s", __FUNCTION__);
}


  • 鉴于存在 NSURLAuthenticationChallengeSender ,如果您预计会遇到挑战(Google网站无法提供),那么您显然会相应地处理它:

  • Given the presence of NSURLAuthenticationChallengeSender, if you're expecting a challenge (which you won't get with Google web site) then you'd obviously handle it accordingly:

    - (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];
        }
    }
    


  • 顺便说一句,你不想要致电 initWithRequest 或时,rel =nofollow> 启动 方法connectionWithRequest 。只有你做一个简单的 initWithRequest:delegate:startImmediately:并指示它 startImmediately

  • By the way, you do not want to call start method when you use initWithRequest or connectionWithRequest. It's only needed if you do a simple initWithRequest:delegate:startImmediately: and instruct it to not startImmediately.

    无论如何,我使用了以下内容并且工作正常:

    Anyway, I used the following and it works fine:

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


  • 这篇关于NSURLConnection - didReceiveData没有被调用 - iOS&amp;弧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆