1

内部にリダイレクトを含むHTMLを返すリクエストを開始しています。didReceiveData関数が2回呼び出されないのはなぜですか?JSONファイルをダウンロードしようとしています。(iOS6を使用)

    - (void) testdownload{
        NSURL *url = [NSURL URLWithString:@"https://***];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        [connection start];
    }

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d{

        NSString *tmpdata = [[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding];
        NSLog(@"data: %@", tmpdata);
    }

UIWebViewを使用すると、リダイレクトが処理され、JSONファイルが表示されます。この関数で遊んでもうまくいきませんでした:

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse{
    return request;
}
4

2 に答える 2

1

代わりに次のコードを使用してみてください。

- (NSURLRequest *)connection: (NSURLConnection *)inConnection
             willSendRequest: (NSURLRequest *)inRequest
            redirectResponse: (NSURLResponse *)inRedirectResponse;
{
    if (inRedirectResponse) {
        NSMutableURLRequest *r = [[request mutableCopy] autorelease]; // original request
        [r setURL: [inRequest URL]];
        return r;
    } else {
        return inRequest;
    }
}

このSOの質問の詳細

于 2013-01-08T13:54:44.713 に答える
0

代わりにこれを使用しました(ただし、あまり良くありません):

- (IBAction)redirectTest:(id)sender {

        NSURL *url = [NSURL URLWithString:@"http://billstclair.com/html-redirect.html"];
        _request = [NSURLRequest requestWithURL:url];

        UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectNull];
        webview.delegate = self;
        [webview loadRequest:_request];
        [self.view addSubview:webview];

    }


    - (void)webViewDidFinishLoad:(UIWebView *)webView{

        NSString *content = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"];
        NSLog(@"content: %@",content);
    }
于 2013-01-09T10:00:23.323 に答える