/*
// api end point to use when user try to login
// param : email ; User Entered Email
// param : password ; User Entered Password
*/
func loginUserInterface(params : [String : String],completion: @escaping StringCompletion)
{
let url = baseURL + APIManager.loginUserEndPoint
print(url)
Alamofire.request(url, method: .post, parameters: params).responseJSON{
response in
if response.result.isSuccess{
let JSONString : JSON = JSON(response.result.value!)
let code = response.response?.statusCode
if (code == 401)
{
completion(false,JSONString,401)
}
else {
completion(true,JSONString,code!)
}
}
else{
let errorJSON: JSON = JSON(response.result.error!)
completion(false,errorJSON,503)
}
}
}
登录用户界面方法来自API管理器。
从loginUser()
调用功能ViewController
。
func loginUser()
{
showLoader()
let params : [String : String] = ["email" : loginEmailTF.text!, "password" : loginPasswordTF.text!]
APIManager.sharedInstance.loginUserInterface(params: params){ (success, data, statusCode) in
self.hideLoader()
if success {
print(data)
}
else
{
print(statusCode)
switch statusCode
{
case 401:
self.showInfo(title: "Error", message: "Incorrect Email or password")
break
case 404:
self.showInfo(title: "Error", message: "No Response from server, Please try again later")
break
case 500:
self.showInfo(title: "Error", message: "No Response from server, Please try again later")
break
case 503:
self.showInfo(title: "Error", message: "No Active Internet Connection")
break
default:
self.showInfo(title: "Error", message: "No Response from server, Please try again later")
}
}
}
}
功能showInfo()
func showInfo(title: String, message: String)
{
let alert2 = UIAlertController(title: title, message: message, preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .default) {
(UIAlertAction) in
alert2.dismiss(animated: false, completion: nil)
}
alert2.addAction(okAction)
self.present(alert2, animated: false, completion: nil)
}
功能showLoader()
func showLoader()
{
let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
loadingIndicator.hidesWhenStopped = true
loadingIndicator.style = UIActivityIndicatorView.Style.gray
loadingIndicator.startAnimating();
alert.view.addSubview(loadingIndicator)
present(alert, animated: false, completion: nil)
}
功能hideLoader()
func hideLoader()
{
alert.dismiss(animated: false, completion: nil)
}
注]当状态代码为401时,loader和showInfo都可以正常工作。但是当状态代码为503时,我会收到以下警告。
2019-10-07 16:56:45.069579 + 0500定义的形状[1859:827702]无法结束BackgroundTask:不存在标识符为1(0x1)的后台任务,或者它可能已经结束了。打破UIApplicationEndBackgroundTaskError()进行调试。
XCode版本10.1(10B61)Swift 4.2
无法结束BackgroundTask:不存在标识符为1(0x1)的后台任务,或者它可能已经结束。中断UIApplicationEndBackgroundTaskError()以进行调试。
这只是弹出的一则令人讨厌的控制台消息。忽略它。它不会影响代码的功能。