Holen Sie sich den http-Antwortcode von jeder UIWebView-Anfrage

Ich muss den Antwortstatuscode überprüfen, während ich eine der URLs in der Webansicht lade. Im Moment können wir jede Webanwendung berücksichtigen, die ich in der Webansicht lade. Daher muss ich jede Anfrage mit dieser Webansicht aufspüren und den Antwortcode entsprechend überprüfen. Um den Antwortcode zu finden, muss NSUrlConnection in der Delegatmethode "shouldStartLoadWithrequest" von uiwebview verwendet werden. Etwas wie das -

- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
        navigationType:(UIWebViewNavigationType)navigationType{
      NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
      if (conn == nil) {
         NSLog(@"cannot create connection");
      }
      return YES;
}

Und NSURLConnectionDelegate als -

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    NSLog(@"connection:didReceiveResponse");
    [self log:@"received response."];
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    int status = [httpResponse statusCode];
     NSLog(@"http status code: %d", status);
    if (status == 200) {
      NSLog(@"Response OK");
    }
    else {
       NSLog(@"Response is not OK");
    }
}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
   NSLog(@"connection:didReceiveData");
}

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"connection:didFailWithError - %@", error);
}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection{
   NSLog(@"connectionDidFinishLoading");
}

In diesem Prozess befinden sich jedoch 2 Aufrufe auf dem Server. Eine für die UIWebview und eine für die NSUrlConnection. Ich brauche nur einen einzigen Anruf an den Server, auf dem ich den Statuscode finden könnte. Kann mich bitte jemand dabei leiten?

Antworten auf die Frage(1)

Ihre Antwort auf die Frage