【问题标题】:a solution to retrieve information when they're no internet connection没有互联网连接时检索信息的解决方案
【发布时间】: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 NSURLSession tutorial Swift”,你无疑会找到很多这个主题的例子。
  • @Rob 我实际上在我的 SOAP 请求函数中使用了 URLSession

标签: swift


【解决方案1】:

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

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

【讨论】:

  • 后台获取不是解决这个特定问题的好方法。背景URLSession 是要走的路。
  • URLSession 没有解决app被用户杀死的问题。我相信两者的结合应该可以满足 Ben 的需求。 URLSession 与用户不杀死应用程序的所有快乐路径的背景配置。以及后台获取,以防应用被用户明确杀死。
  • IIRC,如果用户杀死应用程序,它也会阻止进一步的后台获取。
  • 作为开发人员,我们会尽力覆盖。有办法阻止后台获取这一事实并不意味着我们不应该添加该功能,因为它对某些用户无用。在这里,我们必须决定是覆盖 80% 的允许后台获取的用户,还是应该覆盖 0% 的杀死应用程序的用户。我宁愿尝试获得尽可能多的用户。所以 URLSession + background fetch 仍然是最好的选择。它并不涵盖 100% 的案例,但它是我们所拥有的,而且它肯定涵盖了我们可以涵盖的所有案例。在我看来,我们无能为力。
  • 我了解了您的链接,所以它谈论的是 URLSession 使用的背景配置...但是我使用数据任务,我仍然可以找到解决方案吗?请查看我的编辑以查看我的 SOAP 函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-26
相关资源
最近更新 更多