[英]How to check if user already register or not while registration in swift
我正在尝试在注册时检查用户是否已在我的应用中注册。 对于每次成功注册,我都会在LoginViewcontroller
url 中获得一个 UID,并将该 UID 保存在钥匙串包装器中。 我正在检索RegistrationViewController
regesterservice 中的 UID 值,但是当我尝试检查该 UID 时,我总是得到nil
。 为什么..? 如何检查用户是否已经注册? 请帮我。
在LoginViewcontroller
登录服务中,我正在保存 UID 值,如下所示:
self.Uid = json["id"] as? String
KeychainWrapper.standard.set(emailL ?? "", forKey: "user_email")
KeychainWrapper.standard.set(self.Uid!, forKey: "Uid")
在这里,在RegisterViewController
中,我检索了 UID 值。 但是对于已经注册的人,我在 UID 中也得到了“零”。 为什么?
do {
let userId: String? = KeychainWrapper.standard.string(forKey: "Uid")
print("login userid \(userId)")
if userId != nil{
AlertFun.ShowAlert(title: "Title", message: "user exist", in: self)
} else {
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! [String: Any]
print("go to otp service!!!")
self.otpField = json["otp"] as? Int
}
} catch {
print("error")
}
如何查询已注册人员的 UID?
在第一个代码块中,来自 json 的 Uid 变量与在钥匙串包装器中设置的 Uid 属性不同。
var Uid = json["id"] as? String // `var Uid` is a local variable
KeychainWrapper.standard.set(self.Uid!, forKey: "Uid") // `self.Uid` is a property on self
您可以通过将属性设置为 self 而不是创建单独的变量来修复它
self.Uid = json["id"] as? String
KeychainWrapper.standard.set(self.Uid!, forKey: "Uid")
编辑:
对不起,我不太明白你在说什么。 但我会尝试像这样设置断点和调试,看看哪个语句返回 nil:
po json
po json["id"]
po json["id"] as? String
po self.Uid
po KeychainWrapper.standard.string(forKey: "Uid")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.