我从JSON获得成功和失败状态,我能够检查成功状态,但是在这里无法检查登录中的失败状态情况。登录页面包含PhoneNumber和Password文本字段。
在这里,如果我输入未注册的电话号码,则需要在消息中显示以下消息:
[“状态”:失败,“ msg”:电话号码不存在]
如果我输入的电话号码密码错误,则需要在消息下方显示以下消息:
[“状态”:失败,“ msg”:您输入了错误的密码]
如果我输入了错误的电话号码和密码,则需要在消息下方显示以下消息:
[“状态”:失败,“ msg”:无效的凭据]
但是如果我也输入了错误的密码或未注册的电话号码,我一直无法获得所有Invalid Credentials
的条件。
在这里的注册页面中,我得到uid
以便成功使用电话号码和密码进行注册:
do {
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! [String: Any]
print("the json of otppppppppp \(json)")
DispatchQueue.main.async {
if (self.otpTextField.text == String(self.otpField ?? 12)){
print("registration successfullllll...")
let mobileNum = json["mobile_number"] as! [String : Any]
self.regUid = mobileNum["id"] as? String
let name = mobileNum["name"] as? String
let phNum = mobileNum["username"] as? String
DispatchQueue.main.async {
KeychainWrapper.standard.set(self.regUid!, forKey: "regUid")
KeychainWrapper.standard.set(phNum!, forKey: "user_phnum")
KeychainWrapper.standard.set(self.passwordTextField.text!, forKey: "user_password")
}
let loginVC = self.storyboard?.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
self.present(loginVC, animated: true)
}
}
}
在登录页面中,无法检查电话号码是否已注册以及注册手机是否使用了错误的密码。
我一直在显示Invalid Credentials
失败状态:
do {
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! [String: Any]
print("the json of loginnnnnn \(json)")
var loginStatus = json["status"] as? String
DispatchQueue.main.async {
if loginStatus == "Failed"
{
AlertFun.ShowAlert(title: "", message: "Invalid Credentials", in: self)
}
else{
self.Uid = json["id"] as? String
let emailL = json["user_email"] as? String
print("login uid \(String(describing: self.Uid))")
KeychainWrapper.standard.set(emailL ?? "", forKey: "user_email")
let saveUserId: Bool = KeychainWrapper.standard.set(self.Uid!, forKey: "Uid")
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let navigationController = mainStoryBoard.instantiateViewController(withIdentifier: "HomeNavigation")
let appDelagate = UIApplication.shared.delegate
appDelagate?.window??.rootViewController = navigationController
}
}
}
请帮助我在登录页面中检查以上条件。
答案 0 :(得分:1)
据我了解,您希望根据从JSON获得的状态显示不同的消息。
您可以简单地做到这一点。
AlertFun.ShowAlert(title:“”,消息: json [“ msg”] ,在:self)
为了安全起见,应使用保护语句解开json [“ msg”]的值,并将其显示在Alert中。