3

私は現在、TwitterストリーミングAPIを試していますが、でストリームを取得しようとしていますNSURLConnection。ツイッターでは動作しないので、すべてを簡略化してグーグルのウェブサイトからソースコードを取得しようとしましたが、これも動作しません。

接続は開始および終了しますが、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!");
}
4

3 に答える 3

2

いくつかの考え。

  1. あなたの宣言はconnectionDidFinishLoading正しく見えません。標準メソッドにはパラメータNSURLConnectionDataDelegateがありません。destinationURL

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        NSLog(@"%s", __FUNCTION__);
    }
    
  2. の存在を考えるとNSURLAuthenticationChallengeSender、チャレンジを期待している場合(Google Webサイトでは得られない)、明らかにそれに応じて処理します。

    - (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. ちなみに、またはstartを使用するときにメソッドを呼び出したくありません。単純なことをして、そうしないように指示した場合にのみ必要です。initWithRequestconnectionWithRequestinitWithRequest: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);
    }
    
于 2013-01-28T04:25:00.663 に答える
0

誰かがここでそれが彼らのSOAPフォーマットの NSURLConnectionデリゲートメソッドであると言いました:didReceiveDataは呼び出されませんでした...なぜ?? (iPhone SDK)

これはまたあなたが探しているものかもしれ ませんNSURLConnectiondidReceiveDataは呼ばれていません

于 2013-01-27T23:33:44.907 に答える
0

NSURLConnectionローカル変数connectionは、の終わりでスコープ外になりviewDidLoadます。ビューコントローラーにプロパティを追加して、NSURLConnection変数をスコープ内に保持します。

于 2013-01-27T23:47:28.437 に答える