Note that there are some explanatory texts on larger screens.

plurals
  1. POGet http response code in ios when using sendSynchronousRequest
    text
    copied!<p>I have a working http GET without using any third party bits, I am new to iOS so this was a struggle to setup initially. My code looks like:</p> <pre><code>-(NSString *) SendGetRequestToRest:(NSString *)urlEndString { NSString *userName = @"userN"; NSString *password = @"PassW"; NSString *urlBaseString = @"http://someurl.co.uk/"; NSString *urlString = [NSString stringWithFormat:@"%@%@", urlBaseString, urlEndString]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"GET"]; NSString *str1 = [NSString stringWithFormat:@"%@:%@", userName, password]; NSString *encodedString = [self stringByBase64EncodingWithString:str1]; [request addValue:[NSString stringWithFormat:@"Basic %@",encodedString] forHTTPHeaderField:@"Authorization"]; NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *str = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; NSLog(@"str: %@", str); return str; } </code></pre> <p>What I need to do is track when the status code of the http GET is not a nice 200, I saw <a href="https://stackoverflow.com/questions/12238377">How to check status of web server in iOS?</a> and this looks promising i.e. add this:</p> <pre><code>-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response { if ([response statusCode] == 404) { /// do some stuff } } </code></pre> <p>But I cant see how to connect this up to</p> <pre><code>NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; </code></pre> <p>as it doesnt accept a delegate?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload