【发布时间】:2017-01-16 10:16:58
【问题描述】:
在我的应用中,我有一个表单,用户可以填写该表单并通过 SOAP 请求将其发送给我们。
发送的表格:
但我有一个问题,我没有互联网连接,我无法使用我的 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()
}
【问题讨论】:
-
这要清楚得多。谢谢。
-
@Rob 我觉得我的处境很困难,我不想发送诸如“他们没有互联网连接,稍后再来”之类的消息
-
请参阅 iOS 应用程序编程指南:后台操作中的 Downloading Content in the Background。 虽然该部分是为下载而编写的,但它同样适用于上传。而且,坦率地说,无论如何,您都可以将您的 SOAP 请求视为下载(构建
URLRequest,“下载”是您在 Internet 连接建立后发送请求后收到的服务器响应)。 -
如果你在谷歌上搜索“Background
NSURLSessiontutorial Swift”,你无疑会找到很多这个主题的例子。 -
@Rob 我实际上在我的 SOAP 请求函数中使用了 URLSession
标签: swift