简体   繁体   中英

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. 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. 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:

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. But for already registered persons, I'm also getting `nil in 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?

In the first block of code, the Uid variable from the json is different from the Uid property being set in the keychain wrapper.

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.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:

po json
po json["id"]
po json["id"] as? String
po self.Uid
po KeychainWrapper.standard.string(forKey: "Uid")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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