Swift 2 SOAP Web服务调用

时间:2015-12-01 20:31:23

标签: ios swift web-services soap swift2

我开始将旧的Objective-c SOAP Web服务代码迁移到Swift 2.但是我失败了,因为我从未得到过使用Swift 2的响应,这最终让我疯了。

我开始隔离事物并遵循基于Swift 1.2的Soap温度转换器教程(http://webindream.com/soap-with-swift/)。

这是我的Swift 2转换后的ViewController,但connection永远不会成为现实(connection == true从未发生过)。

//
//  ViewController.swift
//  Temperature Converter
//
//  Created by Farhad on 10/24/14.
//  Copyright (c) 2014 Web In Dream. All rights reserved.
//

import UIKit

//Step 1: Add protocol names that we will be delegating.

class ViewController: UIViewController, UITextFieldDelegate, NSURLConnectionDelegate, NSXMLParserDelegate {

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



    @IBOutlet var txtCelsius : UITextField!
    @IBOutlet var txtFahrenheit : UITextField!


    @IBAction func actionConvert(sender : AnyObject) {
        let celcius = txtCelsius.text

        let soapMessage = "<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><CelsiusToFahrenheit xmlns='http://www.w3schools.com/webservices/'><Celsius>\(celcius)</Celsius></CelsiusToFahrenheit></soap:Body></soap:Envelope>"


        let urlString = "http://www.w3schools.com/webservices/tempconvert.asmx"

        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.HTTPMethod = "POST"
        theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) // or false

        let connection = NSURLConnection(request: theRequest, delegate: self, startImmediately: true)
        connection!.start()

        if (connection == true) {
            var mutableData : NSMutableData = NSMutableData()
        }


    }




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

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


    // NSURLConnectionDelegate

    // NSURL



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

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


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

    }


    // NSXMLParserDelegate

    func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
        currentElementName = elementName

    }



    func parser(parser: NSXMLParser!, foundCharacters string: String!) {
        if currentElementName == "CelsiusToFahrenheitResult" {
            txtFahrenheit.text = string
        }
    }



}

互联网上已经有很多关注但没有真正的答案为什么连接永远不会得到Swift 2.至少没有人最终解决我的问题; - (

这就是我再次提出这个问题的原因,希望为社区获得一个可行的版本。那里有没有成功的Swift 2.1 SOAP Web服务调用并且可以共享解决方案?

非常感谢提前 约翰

1 个答案:

答案 0 :(得分:1)

首先,你应该使用NSUrlSession而不是NSUrlConnection,因为这个类在iOS9中已经过时了。

其次,你的连接是NSURLConnection ?,你必须将它与'nil'而不是'true'进行比较。它不是布尔值。所以代码var mutableData : NSMutableData = NSMutableData()永远不会被调用。

如果您在iOS9中进行测试,则必须为应用程序添加一些设置。否则,您将在连接过程中收到错误。请看一下这个主题:How do I load an HTTP URL with App Transport Security enabled in iOS 9?