class ViewController: UIViewController,XMLParserDelegate,URLSessionDelegate{
@IBOutlet var mobileNumberTF: UITextField!
var mutableData : NSMutableData = NSMutableData()
var currentElementName:String = ""
var json:[String:AnyObject] = [:]
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func submitClicked(_ sender: Any) {
let soapMessage="\(self.mobileNumberTF.text!)"
let msgLength = String(describing: soapMessage.characters.count)
let url = URL(string: "http://....asmx")
var request = URLRequest(url: url!)
request.addValue("...com", forHTTPHeaderField: "Host")
request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue(msgLength, forHTTPHeaderField: "Content-Length")
request.httpMethod = "POST"
request.httpBody = soapMessage.data(using: String.Encoding.utf8, allowLossyConversion: false)
let session = URLSession.shared
let task = session.dataTask(with: request) { (data, resp, error) in
guard error == nil && data != nil else{
print("connection error or data is nill")
return
}
if resp != nil {
self.mutableData.length = 0;
}
print(self.mutableData)
let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
self.mutableData.append(data!)
let xmlParser = XMLParser(data: self.mutableData as Data)
xmlParser.delegate = self
xmlParser.parse()
xmlParser.shouldResolveExternalEntities = true
print(dataString ?? "omg")
}
task.resume()
}
//xml parser
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
currentElementName = elementName
print(elementName)
}
func parser(_ parser: XMLParser, foundCharacters string: String) {
if currentElementName == "SendResult" {
var newString = string.replacingOccurrences(of: "[", with: "")
newString = newString.replacingOccurrences(of: "]", with: "")
print("string= \(newString)")
let data = newString.data(using: .utf8)
do{
let j = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)
print(j)
//Store response in NSDictionary for easy access
let dict = j as? NSDictionary
let amount = "\(dict!["Amount"]!)"
print(amount)
}catch {
print(error)
}
}
}
}
@IBOutlet var mobileNumberTF: UITextField!
var mutableData : NSMutableData = NSMutableData()
var currentElementName:String = ""
var json:[String:AnyObject] = [:]
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func submitClicked(_ sender: Any) {
let soapMessage="\(self.mobileNumberTF.text!)"
let msgLength = String(describing: soapMessage.characters.count)
let url = URL(string: "http://....asmx")
var request = URLRequest(url: url!)
request.addValue("...com", forHTTPHeaderField: "Host")
request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue(msgLength, forHTTPHeaderField: "Content-Length")
request.httpMethod = "POST"
request.httpBody = soapMessage.data(using: String.Encoding.utf8, allowLossyConversion: false)
let session = URLSession.shared
let task = session.dataTask(with: request) { (data, resp, error) in
guard error == nil && data != nil else{
print("connection error or data is nill")
return
}
if resp != nil {
self.mutableData.length = 0;
}
print(self.mutableData)
let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
self.mutableData.append(data!)
let xmlParser = XMLParser(data: self.mutableData as Data)
xmlParser.delegate = self
xmlParser.parse()
xmlParser.shouldResolveExternalEntities = true
print(dataString ?? "omg")
}
task.resume()
}
//xml parser
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
currentElementName = elementName
print(elementName)
}
func parser(_ parser: XMLParser, foundCharacters string: String) {
if currentElementName == "SendResult" {
var newString = string.replacingOccurrences(of: "[", with: "")
newString = newString.replacingOccurrences(of: "]", with: "")
print("string= \(newString)")
let data = newString.data(using: .utf8)
do{
let j = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)
print(j)
//Store response in NSDictionary for easy access
let dict = j as? NSDictionary
let amount = "\(dict!["Amount"]!)"
print(amount)
}catch {
print(error)
}
}
}
}
Sign up here with your email
ConversionConversion EmoticonEmoticon