Swift2中的Soap服务

时间:2015-12-23 13:11:01

标签: ios swift soap

我正在使用新版Swift为我的Soap服务做更新,但我不知道如何解决这些错误:

class ViewController: UIViewController, UITextFieldDelegate, NSURLConnectionDelegate, NSXMLParserDelegate {

    var mutableData: NSMutableData = NSMutableData.alloc()
    var currentElementName: NSString = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        var soapMessage = "<?xml version='1.0' encoding='UTF-8'?> ...</SOAP-ENV:Envelope>"



        var urlString = "https://..."

        var url = NSURL(string: urlString)
        var theRequest = NSMutableURLRequest(URL: url!)
        var msgLength = String(count(soapMessage))

        theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
        theRequest.addValue(msgLength, forHTTPHeaderField: "Content-Length")
        theRequest.addValue("urn:ResponseAction", forHTTPHeaderField: "SOAPAction")
        theRequest.HTTPMethod = "POST"
        theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
        var connection = NSURLConnection(request: theRequest, delegate: self, startImmediately: true)
        connection?.start()
        if(connection == true) {
            var mutableData : Void = NSMutableData.initialize()
        }
    }


    func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
        mutableData.length = 0;
    }

    func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
        mutableData.appendData(data)
    }


    func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge)
    {
        challenge.sender.useCredential(NSURLCredential(forTrust: challenge.protectionSpace.serverTrust), forAuthenticationChallenge: challenge)
        challenge.sender.continueWithoutCredentialForAuthenticationChallenge(challenge)
    }

    func connectionDidFinishLoading(connection: NSURLConnection!) {
        let xmlParser = NSXMLParser(data: mutableData)
        xmlParser.delegate = self
        xmlParser.parse()
        xmlParser.shouldResolveExternalEntities = true

    }

    func parser(parser: NSXMLParser,
        didStartElement elementName: String,
        namespaceURI: String?,
        qualifiedName qName: String?,
        attributes attributeDict: [NSObject : AnyObject]) {
        currentElementName = elementName
    }

    func parser(parser: NSXMLParser, foundCharacters string: String) {
        if currentElementName == "LoginReturn" {
            print(string)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

错误:

  

/用户/ informatiqueresponis /桌面/ ...   2 / SoapSwift / ViewController.swift:28:32:无法调用&#39; count&#39;与   类型&#39;(字符串)&#39;

的参数列表      

/用户/ informatiqueresponis /桌面/ ...   2 / SoapSwift / ViewController.swift:66:10:Objective-C方法   &#39;解析器:didStartElement:的namespaceURI:的qualifiedName:属性:&#39;   方法提供   &#39;解析器(:didStartElement:的namespaceURI:的qualifiedName:属性:)&#39;   与可选的需求方法冲突   &#39;解析器(:didStartElement:的namespaceURI:的qualifiedName:属性:)&#39;在   protocol&#39; NSXMLParserDelegate&#39;

     

/用户/ informatiqueresponis /桌面/ ...   2 / SoapSwift / ViewController.swift:13:52:&#39; alloc()&#39;是不可用的   Swift:改为使用对象初始化器

     

/用户/ informatiqueresponis /桌面/ ...   2 / SoapSwift / ViewController.swift:54:19:可选类型的值   &#39; NSURLAuthenticationChallengeSender&#39?;没有打开;你的意思是   使用&#39;!&#39;或者&#39;?&#39;?

     

/用户/ informatiqueresponis /桌面/ ...   2 / SoapSwift / ViewController.swift:54:92:可选类型的值   &#39; SecTrust&#39?;没有打开;你的意思是使用&#39;!&#39;或者&#39;?&#39;?

     

/用户/ informatiqueresponis /桌面/ ...   2 / SoapSwift / ViewController.swift:55:19:可选类型的值   &#39; NSURLAuthenticationChallengeSender&#39?;没有打开;你的意思是   使用&#39;!&#39;或者&#39;?&#39;?

我对代码进行了更正:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, NSURLConnectionDelegate, NSXMLParserDelegate {

    var mutableData: NSMutableData = NSMutableData()
    var currentElementName: NSString = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let soapMessage = "..."



        let urlString = "https://..."

        let url = NSURL(string: urlString)
        let theRequest = NSMutableURLRequest(URL: url!)
        let msgLength = String(soapMessage.characters.count)

        theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
        theRequest.addValue(msgLength, forHTTPHeaderField: "Content-Length")
        theRequest.addValue("urn:ResponseAction", forHTTPHeaderField: "SOAPAction")
        theRequest.HTTPMethod = "POST"
        theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
        let connection = NSURLConnection(request: theRequest, delegate: self, startImmediately: true)
        connection?.start()
        if(connection == true) {
            let mutableData : Void = NSMutableData.initialize()
        }
    }


    func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
        mutableData.length = 0;
    }

    func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
        mutableData.appendData(data)
    }


    func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge)
    {
        challenge.sender!.useCredential(NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!), forAuthenticationChallenge: challenge)
        challenge.sender!.continueWithoutCredentialForAuthenticationChallenge(challenge)
    }

    func connectionDidFinishLoading(connection: NSURLConnection!) {
        let xmlParser = NSXMLParser(data: mutableData)
        xmlParser.delegate = self
        xmlParser.parse()
        xmlParser.shouldResolveExternalEntities = true

    }

    func parser(parser: NSXMLParser,
        didEndElement elementName: String,
        namespaceURI: String?,
        qualifiedName qName: String?,
        attributes attributeDict: [NSObject : AnyObject]) {
        currentElementName = elementName
    }

    func parser(parser: NSXMLParser, foundCharacters string: String) {
        if currentElementName == "LoginReturn" {
            print(string)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

但是,当我运行我的应用程序时,我什么都没得到

0 个答案:

没有答案