NSURLConnection - didReceiveData didn't get called - iOS & ARC

孤者浪人 提交于 2020-01-24 10:24:09

问题


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

回答1:


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



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/14553538/nsurlconnection-didreceivedata-didnt-get-called-ios-arc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!