NSURLConnection - didReceiveData not called - iOS & ARC

I am currently experimenting with the twit streaming api and I am trying to get the stream from NSURLConnection

. Since it doesn't work with twitter, I simplified everything and tried to get some source code from google site but that won't work either.

The connection starts and ends, but does not call the delegate didReceiveData

. I'm sure I missed something. Hope you guy can help me!

In the title: @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


source to share


3 answers


A few thoughts.



  • Your ad connectionDidFinishLoading

    doesn't look right. The standard NSURLConnectionDataDelegate

    method has no parameter destinationURL

    :

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

  • Given the availability NSURLAuthenticationChallengeSender

    , if you're expecting a call (which you don't get with Google's website), you will 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];
        }
    }
    
          

  • By the way, you don't want to call the method start

    when using initWithRequest

    or connectionWithRequest

    . This is only necessary if you do a simple one initWithRequest:delegate:startImmediately:

    and instruct it not to startImmediately

    .

  • Anyway, I used the following and it works great:

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

+2


source


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



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

0


source


The local variable NSURLConnection connection

is out of scope at the end viewDidLoad

. Add a property to the ViewController to hold the NSURLConnection variable in scope.

0


source







All Articles