0

net web service that returns true or false but i don't know how to catch that response in my IOS App.

My service updates data in a database and i know it works the data gets updated it's catch the response that is the problem, i like to know so i can tell the user if something went wrong.

For those of you that know c# its a bool method, just simple try catch and return true or false.

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    //What to write here to catch my true or false
    if(response) {
       //true
    } else {
       //false
    }
}

Thank you for your help

Popeye
  • 11,839
  • 9
  • 58
  • 91
Madde Persson
  • 413
  • 1
  • 6
  • 26

2 Answers2

0

You should check the response's HTTP status code, e.g.:

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

The status code for a successful request uses the range [200..299].

For example a successful GET request would be indicated with a 200 (OK).

A successful POST request will be indicated with a 201 (Created).

A successful DELET request will be indicated with a 204 (No Content)..

See also: wiki List of HTTP status codes.

Furthermore, you need to check the kind of data the server sent to you:

NSString* mimeType = [response MIMEType];

The mime type has been sent by the server in the Content-Type header of the response.

See also wiki MIME Internet Media Type

What you actually get fully depends on your request AND the server.

For example, the server may always answer with a JSON as content type. In this case, the header Content-Type of the response would be application/json. The actual JSON which represents the answer, will be related to the status code as well.

In order to provide a nice human readable message to the user, you need to consult the web service API and figure out how it is specified. Certain web service APIs may have a considerable large API. Unfortunately, some web services lack a comprehensive documentation.

CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67
  • Thank you, the response is 200 in both cases, if my service returns false the response is 200 and if its true the response is 200. the service return json, but its only on word false or true, if i test it in the browser the text true or false comes up no json syntax like {}... – Madde Persson Feb 27 '14 at 13:07
  • @MaddePersson What is the MIME type of the response in this case? Hint: you may explicitly tell the server that you want a JSON through setting an `Accept` header in your request: `[urlRequest setValue: @"application/json; charset=utf-8" forHTTPHeaderField:@"Accept"]; `. – CouchDeveloper Feb 27 '14 at 13:45
  • @MaddePersson IFF you got JSON, you might getting something like this: `{"result": true}` - which is a proper JSON (the details depend on the web service). You can create a representation of this JSON using NSJSONSerialization. See related answer: http://stackoverflow.com/questions/22063651/how-to-update-json-file-from-web-server/22064643#22064643 – CouchDeveloper Feb 27 '14 at 13:48
  • It's JSON: [OperationContract] [WebGet(UriTemplate = "UpdateNote/{NoteID}/{Note}", ResponseFormat = WebMessageFormat.Json)] //[WebGet(ResponseFormat = WebMessageFormat.Json)] bool UpdateNote(string NoteID, string Note); – Madde Persson Feb 27 '14 at 14:02
  • A single "true" isn't JSON. So, there's something bogus. Can you print the MIME type of the response to the console, e.g. in method `connection:didReceiveResponse:` implement these statements: `NSString* mimeType = [(NSHTTPURLResponse*)response MIMEType]; NSLog(@"MIME: %@", mimeType);` – CouchDeveloper Feb 27 '14 at 14:08
  • Thanks you for your help my Question got answered by sage444. – Madde Persson Feb 27 '14 at 14:25
  • @MaddePersson OK; I thought you already _had_ implemented it - that's mandatory. Otherwise, where would you know from that the response is "true" or "false" ? ;) – CouchDeveloper Feb 27 '14 at 14:39
  • No. Im a newbe at this, started like 2 weeks ago. Im really a web developer trying to leran new things – Madde Persson Feb 27 '14 at 19:02
  • @MaddePersson Yeah - Cocoa is a little bit different. The NSHTTPURLResponse does not contain the body data. Strange? No! If you think about it, that makes absolute sense ;) Anyway good luck and enjoy iOS. – CouchDeveloper Feb 27 '14 at 19:13
0

You should implementconnection:didReceiveData: to get and save NSData and – connectionDidFinishLoading: where you can interpret received data as BOOL.

basically didReceiveResponse: only get to you know about server response to your request not the entire answer.

sage444
  • 5,661
  • 4
  • 33
  • 60
  • Thank you i implemented those functions and i can recive data, do you have an example how to convert it to bool in the connection did finishloading function? – Madde Persson Feb 27 '14 at 13:52
  • convert data into string with `[NSString initWithData:encoding:]` and see what you get, is can be `0/1` or `true/false` in any way this should help – sage444 Feb 27 '14 at 14:01