Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 函数中的dataTaskWithRequest(soap)_Ios_Swift_Soap - Fatal编程技术网

Ios 函数中的dataTaskWithRequest(soap)

Ios 函数中的dataTaskWithRequest(soap),ios,swift,soap,Ios,Swift,Soap,我正在进行SOAP调用,但在调试时,但在执行task.resume()时,它不会在第一次进入dataTaskWithRequest,所以我继续调试,直到最后它才进入dataTaskWithRequest而不调用,我不明白它为什么会这样做,因为我正在将它用于函数?代码: func soapCall(fonction:String, messageSoap:String) -> Int{ if Reachability.isConnectedToNetwork() == true

我正在进行SOAP调用,但在调试时,但在执行task.resume()时,它不会在第一次进入dataTaskWithRequest,所以我继续调试,直到最后它才进入dataTaskWithRequest而不调用,我不明白它为什么会这样做,因为我正在将它用于函数?代码:

func soapCall(fonction:String, messageSoap:String) -> Int{


    if Reachability.isConnectedToNetwork() == true {

        let soapRequest = AEXMLDocument()
        let attributes = ["xmlns:SOAP-ENV" : "http://schemas.xmlsoap.org/soap/envelope/", "xmlns:ns1" : "urn:contactFile", "xmlns:xsd" : "http://www.w3.org/2001/XMLSchema", "xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance", "xmlns:SOAP-ENC" : "http://schemas.xmlsoap.org/soap/encoding/", "SOAP-ENV:encodingStyle" : "http://schemas.xmlsoap.org/soap/encoding/"]
        let envelope = soapRequest.addChild(name: "SOAP-ENV:Envelope", attributes: attributes)
        let body = envelope.addChild(name: "SOAP-ENV:Body")
        let sendLead = body.addChild(name: "ns1:"+fonction)
        sendLead.addChild(name: "xml", attributes: ["xsi:type" : "xsd:string"], value: messageSoap)

        let soapMessage = soapRequest.xmlString

        let messageSoapMinify = minify(soapMessage)




        //        URL Soap
        let urlString = soapServiceURL
        let url = NSURL(string: urlString)
        let theRequest = NSMutableURLRequest(URL: url!)

        //        Taille SoapMessage
        let msgLength = String(messageSoapMinify.characters.count)

        //        Soap Login
        let username = soapUsername
        let password = soapPassword
        let loginString = NSString(format: "%@:%@", username, password)
        let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
        let base64LoginString = loginData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)

        //        Requete Soap
        theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
        theRequest.addValue("Keep-Alive", forHTTPHeaderField: "Connection")
        theRequest.addValue(msgLength, forHTTPHeaderField: "Content-Length")
        theRequest.addValue("urn:ResponseAction", forHTTPHeaderField: "SOAPAction")
        theRequest.HTTPMethod = "POST"
        theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
        theRequest.addValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
        //        print("Request is \(theRequest.allHTTPHeaderFields!)")

        let session = NSURLSession.sharedSession()

        let task = session.dataTaskWithRequest(theRequest, completionHandler: {data, response, error -> Void in
            print("Response Login: \(response)")
            let strData = NSString(data: data!, encoding: NSUTF8StringEncoding)!
            print("Body Login: \(strData)")
            if error != nil
            {
                print("Error Login: " + error!.description)
            }
            let parser = NSXMLParser(data: data!)
            parser.delegate = self
            parser.parse()
            if(self.success == "1"){
                self.successSoap = 1
            }

        })


        task.resume()
    }


    return successSoap
}
我成功地接收了数据库中的数据,但在这里它们是task.resume()中的一个错误。在dataTaskWithRequest()中它不会立即进入

编辑:

这里的电话:

    var statutEnvoie = 0

    serviceSoap.soapCall(fonctionSoap, messageSoap: messageSoapSendLead){ (result) in
        if result == 1 {
            print("SoapCall OK !!!!!!!!!!!!!")
            statutEnvoie = 1
        } else {
            print("SoapCall PAS BON !!!!!!!!!!!!!!!!!")
            statutEnvoie = 0
        }
    }

您不能从异步函数返回!您必须使用回调,通常在Swift中称为“完成处理程序”。请看一看,或者是我的答案或是其他答案中的一个。@Eric。我在dataTaskWithRequest中有一个完成处理程序dataTaskWithRequest中的完成是针对dataTaskWithRequest的数据、响应、错误,而不是您的回调。我说的是添加一个回调来获取这个回调获取的数据。就像我举的例子一样。或者喜欢我最近的很多答案。@Eric。看我的编辑,我有同样的错误:(嗯,你差不多到了。你的完成处理程序应该和任务在同一个范围内,我的意思是在任务内。把它放在
if(self.success==“1”){}
块之后,例如,它应该可以工作。
    var statutEnvoie = 0

    serviceSoap.soapCall(fonctionSoap, messageSoap: messageSoapSendLead){ (result) in
        if result == 1 {
            print("SoapCall OK !!!!!!!!!!!!!")
            statutEnvoie = 1
        } else {
            print("SoapCall PAS BON !!!!!!!!!!!!!!!!!")
            statutEnvoie = 0
        }
    }