How to implement SOAP Web service with SWIFT programming language?




Firest Create a function   SoapReq.Swift and past this code. 

   
       
         import UIKit
         import Alamofire
   
        class SoapReq: NSObject, XMLParserDelegate, NSURLConnectionDelegate {
        var mutableData:NSMutableData  = NSMutableData()
        var currentElementName:NSString = ""
        var responseString = ""
        var delegate: writeValueBackDelegate?
        var url = ""
        var soapMe = "<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>"
        func soapmesss(){
           
        }
       
        func soapRequest(xmlStr : String){
            responseString = ""
            let urlString = "http://10.435764387.453:92/Service.asmx"
           
           // let url = URL(string: urlString)
           
            let theRequest = NSMutableURLRequest(url: NSURL(string: urlString)! as URL)
            let msgLength = xmlStr.count
           
            theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
            theRequest.addValue(String(msgLength), forHTTPHeaderField: "Content-Length")
            theRequest.httpMethod = "POST"
            theRequest.httpBody = xmlStr.data(using: String.Encoding.utf8, allowLossyConversion: false) // or false
            if NetworkReachabilityManager()!.isReachable{
                 let session = URLSession(configuration: URLSessionConfiguration.default)
                 let task: URLSessionDataTask = session.dataTask(with: theRequest as URLRequest ) { (mutableData, response, error) in
                    let xmlParser = XMLParser(data: mutableData!)
                    xmlParser.delegate = self
                    xmlParser.parse()
                    xmlParser.shouldResolveExternalEntities = true
                }
                task.resume()
            } else {
                print("No Internet Connection")
                delegate?.writeValueBack(value: "no!")
            }
        }
       
        // NSXMLParserDelegate
       
        func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
            currentElementName = elementName as NSString
        }
        func parserDidEndDocument(_ parser: XMLParser){
            delegate?.writeValueBack(value: responseString)
        }
        func parser(_ parser: XMLParser, foundCharacters string: String) {
            responseString = responseString + string
          }
        }
   
   
   


   

    For use in your Class  :  

       
   
    let soap = SoapReq()
        let me = soap.soapMe + "<soap:Body><QPAY_CheckMasterKey
       xmlns='http://www.fkjvdfj.com/m2b'><MasterKey>"+String(nid)+"</MasterKey>
       <E_AccountNO>"+String(nid)+"</E_AccountNO></QPAY_CheckMasterKey></soap:Body>
       </soap:Envelope>"
        print(me)
        soap.delegate = self
        soap.soapRequest(xmlStr: me)


Comments

Popular posts from this blog

Swift Create Account and Login Using API