简体   繁体   中英

AFNetworking: how to get the response string in swift 2.0

How to get the success and failure to get response string of server?

Basically I just want to call a function when success is true. The parameters are okay.

Here is my code:

manager.POST(urlString, parameters: params, progress: nil, success: { (requestOperation, response) -> Void in
        let result = NSString(data: response as! NSData, encoding: NSUTF8StringEncoding)!
        print(result)


        NSUserDefaults.standardUserDefaults().setObject(["username" , "password"], forKey: "userDetailsArray")
        NSUserDefaults.standardUserDefaults().synchronize()
        self.getHomeVC()



        }) { (requestOperation, NSError) -> Void in

            print("Error" + NSError.localizedDescription)
    }

your success closure should handle that. Looks like it's called (requestOperation, response) . Docs have changed but if I remember correctly there should be a value like response.isSuccess that you can use.

Something like this:

{(requestOperation, response, NSError) -> Void in
    let successValue = response.isSuccess
    if successValue {
    // call your success function
    }
    print("Error" + NSError.localizedDescription)
}

You should find operation status in API response and check for "SUCCESS".

    if "SUCCESS" == Response.value(forKey: "operationStatus") as? String
           {
              // Do whatever you want on success
            }
               else
                  {
                     // If not success.
                   }

Hope this will help. Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM