наконец-то я все понял, код теперь не сильно оптимизирован, но теперь он будет работать нормально. Я добавил код ниже. если это помогает другим.
import Foundation
import SWXMLHash
class ApiMapper: NSObject
{
func callPostAPI(apiname:String, parameter:String, completionBlockSuccess successBlock: @escaping ((XMLIndexer) -> Void), andFailureBlock failBlock: @escaping ((AnyObject) -> Void))
{
var lobj_Request = URLRequest(url: URL(string: apiname)!)
let session = URLSession.shared
var _: NSError?
lobj_Request.httpMethod = "POST"
lobj_Request.httpBody = parameter.data(using: .utf8)
lobj_Request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
lobj_Request.addValue("\(parameter.count)", forHTTPHeaderField: "Content-Length")
let task = session.dataTask(with: lobj_Request, completionHandler: {data, response, error -> Void in
let strData = String(data: data!, encoding: .utf8)
if error != nil
{
print("Error: " + error!.localizedDescription)
}
let xml = SWXMLHash.parse(strData!)
let xmlString = """
\(xml["s:Envelope"]["s:Body"]["ProcessResponse"]["ProcessResult"])
"""
print(xmlString)
let xmlConverted = SWXMLHash.parse(xmlString)
let convertedString = """
\(xmlConverted["ProcessResult"]["ResponseXml"])
"""
let responseXml = SWXMLHash.parse(convertedString)
if let data = response {
print(data)
if let xmll = response{
print("JSON: \(xmll)")
// print(responseXml)
DispatchQueue.global(qos: .background).async {
DispatchQueue.main.async {
successBlock(responseXml as XMLIndexer)
}
}
}
else
{
let dic = ["message":"Something went wrong, Please try again"]
DispatchQueue.global(qos: .background).async {
DispatchQueue.main.async {
failBlock(dic as AnyObject)
}
}
}
}
else
{
let dic = ["message":"Something went wrong, Please try again"]
DispatchQueue.global(qos: .background).async {
DispatchQueue.main.async {
failBlock(dic as AnyObject)
}
}
}
})
task.resume()
}
}