0

im new to ios. I'm making simple login app using soap request. so the problem is when I get a response from the server it looks like

Optional(<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <CreateLoginResponse xmlns="http://tempuri.org/">
            <CreateLoginResult>[{"DelBoyId":36,"DelBoy_Fname":"abc-xyz","Password":"123","DelBoy_Email":"[email protected]","DelBoy_Contact":"123","RoleName":"Admin"}]
            </CreateLoginResult>
        </CreateLoginResponse>
    </soap:Body>
</soap:Envelope>).

and I want to store this key value [{"DelBoyId":36,"DelBoy_Fname":"abc-xyz","Password":"123","DelBoy_Email":"[email protected]","DelBoy_Contact":"123","RoleName":"Admin"}] in session.

how can I do this ? please help me.

thank you.

here is my code

import UIKit

class LoginPageViewController: UIViewController, NSXMLParserDelegate {

    @IBOutlet var txtUserEmail: UITextField!

    @IBOutlet var txtUserPassword: UITextField!

    var webData : NSMutableData?
    var parser : NSXMLParser?
    var flag : Bool?
    var capturedString : String?

    func processTerrorSaftyTips(data : NSData){
        parser = NSXMLParser(data: data)
        parser!.delegate = self
        parser!.parse()

    }


    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }


    @IBAction func loginButtonTapped(sender: AnyObject) {

        let mobile = txtUserEmail.text;
        let password = txtUserPassword.text;


        let soapMessage = String(format: "<?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/'><soap:Body><CreateLogin xmlns='http://tempuri.org/'><mobile>"+mobile!+"</mobile><pasword>"+password!+"</pasword></CreateLogin></soap:Body></soap:Envelope>");

        let url = NSURL(string: "http://23.91.115.57/nagifts/NaGiftsWebService.asmx");

        let theRequest = NSMutableURLRequest (URL: url!);
        let soapLength = String(soapMessage.characters.count)

        theRequest.addValue("text/xml", forHTTPHeaderField: "Content-Type");
        theRequest.addValue(soapLength, forHTTPHeaderField: "Content-Length")
        theRequest.HTTPMethod = "POST";
        theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding,allowLossyConversion: false);

        let urlConnection = NSURLConnection(request: theRequest , delegate: self);

    }

    func connection(connection: NSURLConnection, didFailWithError error: NSError){

    }

    func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse){

        webData = NSMutableData()
    }
    func connection(connection: NSURLConnection, didReceiveData data: NSData){

        webData!.appendData(data)
    }
    func connectionDidFinishLoading(connection: NSURLConnection){

        processTerrorSaftyTips(webData!)
    }

    func parserDidStartDocument(parser: NSXMLParser){

        flag = false
        capturedString = ""
    }
    func parserDidEndDocument(parser: NSXMLParser){

    }

    func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]){

        flag = false
        capturedString = ""
        if elementName == "CreateLoginResult"
        {
            flag = true
        }

    }

    func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?){

        flag = false
        if elementName == "CreateLoginResult"
        {
            print((capturedString!))


        }
    }
    func parser(parser: NSXMLParser, foundCharacters string: String){

        if flag! {
            capturedString = capturedString! + string
        }

    }
    func parser(parser: NSXMLParser, parseErrorOccurred parseError: NSError){

        }

}
2
  • you are not using xcode8? Commented Oct 25, 2016 at 9:59
  • no.currently I'm using Xcode7.3 @sanju Commented Oct 25, 2016 at 11:33

1 Answer 1

1

Swift3:

Declare any array because your response is in array

var arrayFromResult = NSMutableArray()

Next go to Parse DidEndElement and add foundCharacters to Array do like this

 if elementName == "CreateLoginResult"
  {
        print((capturedString!))
    arrayFromResult.add(capturedString)

   }

Usage Of array Or retrieve Dictionary Values from Array

  for var i  in 0..<arrayFromResult.count {

        let dictFromArray = arrayFromResult.object(at: i) as! NSDictionary

        let name = dictFromArray.value(forKey: "DelBoy_Fname") as! String
        print(name)

        let password = dictFromArray.value(forKey: "Password") as! String
        print(password)


    }
Sign up to request clarification or add additional context in comments.

12 Comments

when i add arrayFromResult.add(capturedString)
when i add arrayFromResult.add(capturedString) .error occures "value of type NSMutableArray has nomember add" @Sanju ju
try writing like this : arrayFromResult.add(capturedString as! NSMutableArray)
I think response need to change. ask the server people to change like <Parent><Child></Child><Child></Child>..........</Parent>
Hey in Xcode 7.3 you need to write like this man arrayFromResult.addObject(capturedString)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.