How to read HttpResponseMessage in IOS from web API?

180 Views Asked by At

We have asp.net Web API developed. In Web API controllers there are the Filters which generating HttpResponseMessage as response. These API's are consuming by IOS device application. All actions in API are returning HttpResponseMessage. If Filter executes without any error then execution of action performing properly and getting HttpResponseMessage in IOS code. But if any exceptional case in filter happens say token is invalid then HttpResponseMessage is not able to fetch in IOS. Why should this happening ? Tested in Fiddler and in REST Client, I can see the HttpResponseMessage generated from action as well as from Filters (if fails). IOS getting nil response if Filter fails. Any suggestion? (I am not IOS guy, but IOS team unable to resolving this.)

Update:

let postDataTask : NSURLSessionDataTask = session.dataTaskWithRequest(request) { (data, responce, error) -> Void in

                if (error == nil)
                {
                    let strData = NSString(data: data!, encoding: NSUTF8StringEncoding)
                    let _ : NSError?

                    var json : NSDictionary!
                    do
                    {
                        json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves) as? NSDictionary
                    }
                    catch _ as NSError
                    {}
                    var issucceeded : Bool = false

                    print("Responce - \(json)")


                    if(json != nil)
                    {
                        issucceeded = true

//                        if((json.valueForKey("RESPONSE")) != nil)
//                        {
//                            if(json.valueForKey("RESPONSE") as! NSString == "Success")
//                            {
//                                issucceeded = true
//                            }
//                        }
                        postCompleted(succeeded: issucceeded, responseDict: json)
                    }
                    else // Responce is not in proper JSON format
                    {
                        postCompleted(succeeded: false, responseDict: ["HasErrors":"1","ErrorMessage":k_Alert_24,"TryAgain":"True"])
                    }
                }
                else
                {

                    postCompleted(succeeded: false, responseDict: ["HasErrors":"1","ErrorMessage":k_Alert_24,"TryAgain":"True"])
                }
            }
           postDataTask.resume()
0

There are 0 best solutions below