0
我正在使用其他API登錄我只是使用用戶的電子郵件和密碼發送請求。如果它們在數據庫中匹配,那麼我返回一些Json並快速解析它。這部分工作正常,我現在想要做的是弄清楚如何提醒用戶電子郵件和密碼不匹配。到目前爲止,我得到這個IOS Swift如何更快地返回標籤響應
@IBAction func Login_Action(_ sender: Any) {
var check = 0
let url:URL = URL(string:ConnectionString+"login")!
let Email = email.text!
let Password = password.text!
let session = URLSession.shared
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData
let parameter = "email=\(Email)&password=\(Password)"
request.httpBody = parameter.data(using: String.Encoding.utf8)
session.dataTask(with:request, completionHandler: {(data, response, error) in
if error != nil {
// print(error)
} else {
do {
// if the call is successful then it goes through here otherwise it skips it
check = 1
let parsedData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:Any]
}
DispatchQueue.main.async {
// send them to member page
let next = self.storyboard?.instantiateViewController(withIdentifier: "NextView")
self.present(next, animated: true, completion: nil)
}
}
} catch let error as NSError {
print(error)
if check == 0 {
self.error.isHidden = false
self.error.text = "Incorrect email and/or password"
}
}
}
// If the call is unsuccessful then show message
return
}).resume()
/*
print(check)
if check == 0 {
self.error.isHidden = false
self.error.text = "Incorrect email and/or password"
print("incorrect")
}*/
}
我使用檢查變量作爲標誌它的默認值是0。這意味着電子郵件/密碼組合不起作用,如果電子郵件/密碼組合工作,然後我將它設置爲1。問題是,如果用戶提供了錯誤的憑證,則需要大約5或7秒才能顯示錯誤的電子郵件和/或密碼消息。當我查看通話響應時間時,它會立即返回,但標籤顯示需要7秒鐘。如果我把該.resume外相同的邏輯則出現瞬間不過消息始終是不正確的電子郵件和密碼,檢查是否爲0或1的任何建議將是更好的
嘿羅馬,請改善您的代碼格式 - 很難閱讀它,謝謝!另外:請記住,'guard'是你的朋友:-) –
你需要在關閉窗口*中顯示關於不正確的登錄詳細信息的信息,就像你對下一個場景的演示一樣。記得在主隊列上發送UI更新 – Paulw11
謝謝你會做Paulo和哦OK Paulw現在會這樣做 –