0

I am developing an application in which I need to get the response code of a HTTP URL, The URL returns an xml which is encrypted. I only need to get that encrypted response code into a variable. I've used Alamofire for http request.

Thanks in advance!

Uday Babariya
  • 1,031
  • 1
  • 13
  • 31
Ashique
  • 91
  • 1
  • 4
  • 16
  • 1
    Have you looked at this answer? https://stackoverflow.com/a/30980470/8069241 – maxwell Mar 14 '18 at 08:48
  • I tried it.. But since Iam using swift latest version, it did't work. I've installed SWXML hash using cocoapods. I tried to upgrade the code to swift4, but showing error!! @maxwell – Ashique Mar 14 '18 at 09:56
  • Actually, the URL should returns an xml response, which I am not able to print in console – Ashique Mar 14 '18 at 10:20
  • have you looked at this answer https://stackoverflow.com/questions/30913594/handling-xml-data-with-alamofire-in-swift – Neha Mar 15 '18 at 05:33
  • @Neha I tried that too.. since they are old version code, it didn't work.. I was trying with SWXML itself. I tried to alter the code into the latest methods, still they show error.!!! – Ashique Mar 15 '18 at 05:43
  • I'll give an example of the problem that I face... I have a URL which when called actually returns an XML which is just an encrypted key.(that doesn't matter). Using Alamofire I tried to send a http request with that URL. It shows successful URL hit, But the response Iam getting is something else. I don't get a xml response. – Ashique Mar 15 '18 at 05:57
  • @Neha Thanks Neha and Maxwell for giving a solution. That code just needed some alteration and it worked fine...Thanks for helping me out.. :) – Ashique Mar 15 '18 at 06:21

1 Answers1

1
Alamofire.request("URL Goes here",method:.get)
 .response { response in
    if let data = response.data {
      print(data)
      var xml = SWXMLHash.parse(data)
      print(xml["e1"]["e2"].element?.text)
    }
 }
Ashique
  • 91
  • 1
  • 4
  • 16
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – rollstuhlfahrer Mar 15 '18 at 07:06
  • @rollstuhlfahrer Swift has libraries like SwiftyJSON for JSON handling while XMLParsing and getting XML response is a bit confusing. So in a context where we want to get the XML response of a URL in IOS (Swift), we can use SWXMLHash(i think it's an external third-party library) for doing the same.. It's pretty simple as it explains...! :) – Ashique Mar 16 '18 at 04:23