我打电话给Soap WebSerivce时如何显示活动指示器?

时间:2018-06-19 16:34:51

标签: ios swift xml xcode web-services

他在那里

当用户点击“登录”按钮时,我试图显示活动指示器。我使用了有关如何显示活动指标的教程,但没有任何帮助。

我正在做的是我有一个连接到Microsoft SQL Server的soap Web服务,并且正在调用此Web服务来检查登录。在验证用户输入时我想做些什么来显示活动指示器

这是按钮操作

@IBAction func loginButton(_ sender: Any) {
    activityIndicator.startAnimating()

    guard let usernametext = usernameTextfield.text, let passwordText = passwordTextfield.text else {
        return
    }

    if usernametext.isEmpty || passwordText.isEmpty {
        AlertMessage().showAlertMessage(alertTitle: "Alert!", alertMessage: "Username or password is empty", actionTitle: nil, onAction: nil, cancelAction: "Dismiss", self)
    } else {
        if let language = languangeTextfield.text{
            LoginViewController.languageChosen = setLanguageChosen(languagetextfield: language)
        }

        AuthServices().checkUserId(id: usernametext, password: passwordText, onSeccuss: {
            self.setLanguage()
            self.performSegue(withIdentifier: "showHomePage", sender: nil)
        }, onError: { (error) in
            AlertMessage().showAlertMessage(alertTitle: "Alert!", alertMessage: error, actionTitle: nil, onAction: nil, cancelAction: "Dismiss", self)
        }, activityIndicator: activityIndicator)
    }
}

这是checkUserId函数

func checkUserId(id: String, password: String, onSeccuss: @escaping () -> Void, onError: @escaping (_ ErrorMessage: String) -> Void, activityIndicator: UIActivityIndicatorView){
    arrayOfResult = login.CheckLogin(username: id, password: password, error: "", langid: language, activityIndicator: activityIndicator)

    if !arrayOfResult.isEmpty{
        if arrayOfResult[0] == "0"{
            AuthServices.currentUserId = id
            AuthServices.currentUserName = arrayOfResult[1]
            onSeccuss()
        } else {
            if let error = arrayOfResult[0]{
                onError(error)
            }
        }
    } else {
        print("Oops, something went wrong!")
    }
}

这是urlsession

public func CheckLogin(username:String, password:String, error:String, langid:Int, activityIndicator: UIActivityIndicatorView)-> [String?]{
    var soapReqXML:String = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"

    soapReqXML  += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
    soapReqXML  += " xmlns:xsd =\"http://www.w3.org/2001/XMLSchema\""
    soapReqXML  += " xmlns:soap =\"http://schemas.xmlsoap.org/soap/envelope/\">"
    soapReqXML += " <soap:Body>"
    soapReqXML += "<CheckLogin xmlns=\"http://tempuri.org/\">"
    soapReqXML += "<username>"
    soapReqXML += username
    soapReqXML += "</username>"
    soapReqXML += "<password>"
    soapReqXML += password
    soapReqXML += "</password>"
    soapReqXML += "<error>"
    soapReqXML += error
    soapReqXML += "</error>"
    soapReqXML += "<langid>"
    soapReqXML += String(langid)
    soapReqXML += "</langid>"
    soapReqXML += "</CheckLogin>"
    soapReqXML += "</soap:Body>"
    soapReqXML += "</soap:Envelope>"

    let soapAction :String = "http://tempuri.org/CheckLogin"

    let responseData:Data = SoapHttpClient.callWS(Host : self.Host,WebServiceUrl:self.Url,SoapAction:soapAction,SoapMessage:soapReqXML, activityIndicator: activityIndicator)

    let strVals :[String?] = stringArrFromXML(data : responseData);
    var vals = [String?]()
    for i in 0  ..< strVals.count {
        let xVal =  strVals[i]
        vals.append(xVal)
    }
    let returnValue:[String?] = vals
    return returnValue
}


public class func callWS(Host:String,WebServiceUrl:String, SoapAction:String, SoapMessage:String, activityIndicator: UIActivityIndicatorView)-> Data{
    activityIndicator.startAnimating()
    self.Error = nil;
    self.ErrorString = nil;
    self.StatusCode = nil;
    self.StatusDescription = nil;
    self.ResponseData = nil;
    self.ResponseString = nil;

    var responseData : Data = Data()

    let semaphore = DispatchSemaphore.init(value: 0)
    let url = URL.init(string: WebServiceUrl)
    let req = NSMutableURLRequest(url: url!)

    let session = URLSession.shared
    req.httpMethod = "POST"
    req.httpBody = SoapMessage.data(using: String.Encoding.utf8, allowLossyConversion: false)
    req.addValue(Host, forHTTPHeaderField: "Host")
    req.addValue("text/xml;charset =utf-8", forHTTPHeaderField: "Content-Type")

    let contentLength = SoapMessage.utf8.count
    req.addValue(String(contentLength), forHTTPHeaderField: "Content-Length")
    req.addValue(SoapAction, forHTTPHeaderField: "SOAPAction")

    let task_ = session.dataTask(with: req as URLRequest){ (data, response, error) in
        DispatchQueue.main.async {
            activityIndicator.stopAnimating()
        }
        self.Error=error

        if let httpResponse = response as? HTTPURLResponse {
            self.StatusCode = httpResponse.statusCode
            if httpResponse.statusCode != 200 {
                self.StatusCode=httpResponse.statusCode
                self.ErrorString=httpResponse.description

                responseData = Data()
                self.ResponseData = Data()
            } else {
                responseData = data!
                self.ResponseData = data
                let responseString =  String.init(data: data!, encoding: String.Encoding.utf8)
                self.ResponseString = responseString!
            }
        }
        semaphore.signal()
    }
    task_.resume()

    semaphore.wait()
    return responseData
}

这是情节提要的图片

enter image description here

先谢谢大家

1 个答案:

答案 0 :(得分:0)

假设您的UIActivityIndicatorView实际上已添加到视图层次结构中,听起来您的指标视图可能不是堆栈中最前面的视图。

您需要在情节提要中修复该问题,或者使用UIView的bringSubviewToFront(_ view: UIView)使父视图将其提出。