Soap API Integration in Swift ?

 Web services which is mostly used to communicate each other apps using server, server store value of apps, and give when required. These process is called web services, Here discuss about , how to call an ASMX service in iOS.we will send a Book Name and chapter number to Server then gives response of chapter Name and text Title. 

The Soap API request calling by AFNetworking, and its XML response handle using XMLReader. So Download AFNetworking.  and XMLReader is already in project with updated. You have to follow the steps :  How to use objective c in swift ? After successfully added follow the below steps. 

Step 1 : Create Xcode Project : Go to file >New > Projetc > DemoApp.

Step 2 :  This demo calling Soap API to get All Chapter names of the Book, I have one Soap API, By using this API to get Chapters name from Book.

Step 3 :

Step 4 :  Open above URL in the Browser and get the method GetVerses.

Step 5 :  The best way to get SOAP request : Open : and Paste , after Click on GetVerses and get XML request .

Step 6 : Soap body code:
let soapMessage =  "Genesis1"        
let soapLenth = String(soapMessage.characters.count)
let theUrlString = ""
let theURL = NSURL(string: theUrlString)
Step 7 : Soap body request:
let mutableR = NSMutableURLRequest(URL: theURL!)
mutableR.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
mutableR.addValue("text/html; charset=utf-8", forHTTPHeaderField: "Content-Type")
mutableR.addValue(soapLenth, forHTTPHeaderField: "Content-Length")
mutableR.HTTPMethod = "POST"
mutableR.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding)
Step 8 : AFHTTPRequestOperation:
let manager = AFHTTPRequestOperation(request: mutableR)
manager.setCompletionBlockWithSuccess({ (operation : AFHTTPRequestOperation, responseObject : AnyObject) -> Void in

        var dictionaryData = NSDictionary()
            dictionaryData = try XMLReader.dictionaryForXMLData(responseObject as! NSData)   
            let mainDict = dictionaryData.objectForKey("soap:Envelope")!.objectForKey("soap:Body")!.objectForKey("GetVersesResponse")!.objectForKey("GetVersesResult")   ?? NSDictionary()

            if mainDict.count > 0{

                let mainD = NSDictionary(dictionary: mainDict as! [NSObject : AnyObject])
                self.performSegueWithIdentifier("details", sender: mainD)

                UIAlertView(title: "BooksDetailsq", message: "Oops! No data found.", delegate: nil, cancelButtonTitle: "OK").show()
            print("Your Dictionary value nil")


        }, failure: { (operation : AFHTTPRequestOperation, error : NSError) -> Void in

            print(error, terminator: "")



Step 9 : Download the below project: SoapDemo


Soap API Integration in Swift ? Soap API Integration in Swift ? Reviewed by Kirit Modi on 11:08:00 Rating: 5


  1. AFNetworking no longer has AFHTTPRequestOperation

    1. yes, You can easily update with new AFNetworking class,

  2. Nice. Simple and straight to the point.
    Good Job


Powered by Blogger.