简体   繁体   English

在 swift 注册时如何检查用户是否已经注册

[英]How to check if user already register or not while registration in swift

I'm trying to check if a user is already registered or not in my app when registering.我正在尝试在注册时检查用户是否已在我的应用中注册。 For every successful registration, I'm getting a UID in LoginViewcontroller url and I'm saving that UID in the keychain wrapper.对于每次成功注册,我都会在LoginViewcontroller url 中获得一个 UID,并将该 UID 保存在钥匙串包装器中。 I'm retrieving the UID value in RegistrationViewController regesterservice, but when I'm trying to check that UID I'm getting nil all the time.我正在检索RegistrationViewController regesterservice 中的 UID 值,但是当我尝试检查该 UID 时,我总是得到nil Why..?为什么..? how to check if a user is already registered or not?如何检查用户是否已经注册? Please help me.请帮我。

In LoginViewcontroller login service, I'm saving the UID value like this:LoginViewcontroller登录服务中,我正在保存 UID 值,如下所示:

self.Uid = json["id"] as? String
KeychainWrapper.standard.set(emailL ?? "", forKey: "user_email")
KeychainWrapper.standard.set(self.Uid!, forKey: "Uid")

Here, in RegisterViewController , I retrieve the UID value.在这里,在RegisterViewController中,我检索了 UID 值。 But for already registered persons, I'm also getting `nil in UID.但是对于已经注册的人,我在 UID 中也得到了“零”。 Why?为什么?

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")
}

How can I query a UID of an already registered person?如何查询已注册人员的 UID?

In the first block of code, the Uid variable from the json is different from the Uid property being set in the keychain wrapper.在第一个代码块中,来自 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

you can fix it by setting property to self instead of creating a separate variable您可以通过将属性设置为 self 而不是创建单独的变量来修复它

self.Uid = json["id"] as? String
KeychainWrapper.standard.set(self.Uid!, forKey: "Uid")

edit:编辑:

I'm sorry, I don't quite understand what you're saying.对不起,我不太明白你在说什么。 But I would try setting a breakpoint and debugging like this, and see which statement returns nil:但我会尝试像这样设置断点和调试,看看哪个语句返回 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM