1

我正在尝试在注册时检查用户是否已在我的应用中注册。对于每个成功的注册,我都会在LoginViewcontrollerurl 中获得一个 UID,并将该 UID 保存在钥匙串包装器中。我正在检索RegistrationViewControllerregesterservice 中的 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?

4

1 回答 1

1

在第一个代码块中,来自 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")

于 2019-11-02T08:21:34.457 回答