0

返されるステータス コード (200 OK / 404 ERROR) を監視できるように、NSURLConnection を使用して Web ページに接続するプロジェクトを行っています。ステータス コードとして 404 を受け取った場合はユーザーをトップ URL www.domain.com に送りたいと思います。ステータス コード 200 を受け取った場合は、ページを Web ビューにロードしたいと思います。新しいリクエストを作成することでこの問題のいくつかの実装を見てきましたが、最初のリクエストですでに html を受け取っているので不要だと感じているので、その HTML を webView にロードしたいと思います。

だから私は [webView loadHTMLFromString: baseURL:] を使用しようとしますが、常に機能するとは限りません。 didReceiveData 最後のパケットの乱数は NULL です (2 ~ 10 の間で異なります)。ロードされないのは常に同じ Web ページです。[webView loadRequest:myRequest] を使用してそれらを webView にロードすると、常に機能します。私の実装は次のようになります。おそらく、誰かが私が間違っていることを見ることができます。

ボタンのクリックで最初のリクエストを作成します。

-(IBAction)buttonClick:(id)sender
{
    NSURL *url = [NSURL URLWithString:@"http://www.domain.com/page2/apa.html"];
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:url]
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if( theConnection )
    {
        webData = [[NSMutableData data] retain];
    }
    else
    {

    }
}

次に、リクエストを NSHTTPURLResponse にキャストしてステータス コードにアクセスできるようにし、ステータス コードに応じて Bool を設定することで、didReceiveResponse メソッドのレスポンス コードを監視します。

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{     
    NSHTTPURLResponse *ne = (NSHTTPURLResponse *)response;
    if ([ne statusCode] == 200) {
        ok = TRUE;
    }

    [webData setLength: 0];
}

次に、connectionDidFinnishLoading でブール値を確認します。HTML NSString をログに記録すると、Web ページのソースが取得されるので、それが空の文字列ではないことがわかります。

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSString *html = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];

    NSURL *url = [NSURL URLWithString:@"http://www.domain.com/"];
    if (ok) {
        [webView loadHTMLString:html baseURL:url];
        ok = FALSE;
    }
    else {
        // Create a new request to www.domain.com
    }

}

webData はインスタンス変数で、このように didReceiveData にロードします。

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webData appendData:data];
}
4

2 に答える 2

0

You are mixing two ways of performing connection - synchronous and asynchronous too. If the asynchronous finishes before the synchronous, you reinitialize webData to empty string. So even if synchronous populated webData it would be cleared.

// you create asynchronous connection which starts downloading in background immediately
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
// you create another synchronous connection which will block until the URL is downloaded
[NSURLConnection sendSynchronousRequest:theRequest returningResponse: &responseHeader error: nil];

// assuming your theRequest is valid, theConnection is always non-nil, so this is always TRUE
if (theConnection) {
     // you reset content of webData, so if your synchronous connection has downloaded something, you throw it away here
     webData = [[NSMutableData data] retain];
} else ...

Try to remove unnecessary/buggy call to sendSynchronousRequest:returningResponse:error: and it might work.

Anyway - where do you populate webData with data ? You did implement connection:didReceiveData: and append what comes into webData, right ? If you didn't - well, that's the problem you are seeing.

于 2010-10-27T20:56:13.973 に答える
0

webDataそれがあなたのクラスのインスタンス変数であると仮定しています。あなたの問題は、webDataから取得しているデータに設定していないことだと思いますNSURLConnection。次のようなものを試してください。

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [webData appendData:data];
}

それでもうまくいかない場合は、次のように行のNSLog()後に追加してみてください。NSString *html = [[NSString alloc]...

NSLog(@"HTML: %@", html);

これにより、HTMLを取得しているかどうか、および問題がNSURLConnectionコードにあるのか、にあるのかがわかりますUIWebView IBOutlet

于 2010-10-27T20:58:29.977 に答える