在我的应用程序上,我有一个表单,用户可以填写该表单并将其与SOAP请求一起发送给我们。

发送的表格:

swift - 当他们没有互联网连接时检索信息的解决方案-LMLPHP

但是我有一个问题,我没有互联网连接,无法使用SOAP请求发送数据。

因此,首先我将请求保存在Core Data中,这样就可以了...当用户在应用程序上时,我会执行预定的请求,以查看他们是否是Core Data中发送请求的请求。

但是,如果应用程序未运行怎么办?在Android中,我使用后台服务,但是对于Apple,这是不可能的:

Android Services equivalent in iOS Swift

有人有想法检索数据吗?

香皂功能:

func soapCall(fonction:String, soapMessageParamsTab:[SoapMessageParams], completion: @escaping (_ strData: NSString)-> ()/*, completion: @escaping (_ result: Int, _ resultContactWebId: Int)->()*/){
        let soapRequest = AEXMLDocument()
        let attributes = ["xmlns:SOAP-ENV" : "http://schemas.xmlsoap.org/soap/envelope/", "xmlns:ns1" : namespace, "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)

    for obj in soapMessageParamsTab {
        sendLead.addChild(name: obj.soapName, value: obj.soapValue, attributes: ["xsi:type" : "xsd:"+obj.soapType])
    }
        //sendLead.addChild(name: "xml", value: messageSoap, attributes: ["xsi:type" : "xsd:string"])


        let soapMessage = soapRequest.xmlString
        let messageSoapMinify = minify(soapMessage)


        //        URL Soap
        let urlString = soapServiceURL
        let url = URL(string: urlString)
        var theRequest = URLRequest(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: Data = loginString.data(using: String.Encoding.utf8.rawValue)!
        let base64LoginString = loginData.base64EncodedString(options: NSData.Base64EncodingOptions.lineLength64Characters)

        //        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 = messageSoapMinify.data(using: String.Encoding.utf8, allowLossyConversion: true)
        theRequest.addValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
        //        print("Request is \(theRequest.allHTTPHeaderFields!)")

        let session = Foundation.URLSession.shared

        let task = session.dataTask(with: theRequest, completionHandler: {data, response, error -> Void in

            if error != nil
            {
                print(error?.localizedDescription)
            }

            //print("Response Login: \(response)")
            //print("Error Login: \(error)")
            let strData = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)!
            //print("Body Login: \(strData)")

            completion(strData)
        })


        task.resume()

}

最佳答案

将NSURLSession与后台配置一起使用应该没问题。
注意:如果该应用被用户明确杀死,则计划的请求将被取消。仅当该应用因任何原因(内存不足,崩溃等)而被系统杀死时,此技巧才有效。如果该应用被用户杀死,您将无能为力。后台获取可能是一种解决方案,但将由iOS决定是否以及何时唤醒应用程序以进行机会获取。
这可以帮助您:
https://blog.newrelic.com/2016/01/13/ios9-background-execution/

编辑:
本教程也可能有用:https://www.raywenderlich.com/110458/nsurlsession-tutorial-getting-started

关于swift - 当他们没有互联网连接时检索信息的解决方案,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41674040/

10-09 02:32