Saturday, May 11, 2013

NSHTTPURLResponse localizedStringForStatusCode example ios


localizedStringForStatusCode:

Returns a localized string corresponding to a specified HTTP status code.
+ (NSString *)localizedStringForStatusCode:(NSInteger)statusCode
Parameters
statusCode
The HTTP status code.
Return Value of [NSHTTPURLResponse localizedStringForStatusCode]
A localized string suitable for displaying to users that describes the specified status code.
Example of [NSHTTPURLResponse localizedStringForStatusCode]
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    // cast the response to NSHTTPURLResponse so we can look for 404 etc
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

    if ([httpResponse statusCode] >= 400) {
        // do error handling here
        NSLog(@"remote url returned error %d %@",[httpResponse statusCode],[NSHTTPURLResponse localizedStringForStatusCode:[httpResponse statusCode]]);
    } else {
        // start recieving data
    }
}
Example of [NSHTTPURLResponse localizedStringForStatusCode]
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
    NSInteger statusCode = [HTTPResponse statusCode];

    if (404 == statusCode || 500 == statusCode) {
        //[self.controller setTitle:@"Error Getting Parking Spot ....."];
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];

        [connection cancel];
        NSLog(@"Server Error - %@", [NSHTTPURLResponse localizedStringForStatusCode:statusCode]);
    } else {
        if ([self.requestType isEqualToString:@"GET"])
            [_responseData setLength:0];
    }
}