NSURLConnection - didReceiveData didn't get ca

2019-07-02 08:31发布

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.

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!

In the header: @interface ViewController : UIViewController <NSURLConnectionDataDelegate, NSURLConnectionDelegate, NSURLConnectionDownloadDelegate, NSURLAuthenticationChallengeSender>

And in the body:

- (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条回答
够拽才男人
2楼-- · 2019-07-02 08:36

Your NSURLConnection local variable connection is going out of scope at the end of viewDidLoad. Add a property to your ViewController to hold the NSURLConnection variable in scope.

查看更多
我想做一个坏孩纸
3楼-- · 2019-07-02 08:45

A couple of thoughts.

  1. Your declaration of connectionDidFinishLoading doesn't look right. The standard NSURLConnectionDataDelegate method does not have a destinationURL parameter:

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        NSLog(@"%s", __FUNCTION__);
    }
    
  2. 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];
        }
    }
    
  3. 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.

  4. 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);
    }
    
查看更多
可以哭但决不认输i
4楼-- · 2019-07-02 08:57

Someone said here that it was their SOAP format NSURLConnection delegate method: didReceiveData not called ...Why ?? (iPhone SDK)

This also may be what you are looking for NSURLConnection didReceiveData not called

查看更多
登录 后发表回答