OGeek|极客世界-中国程序员成长平台

标题: ios - 基于角色-Firebase、Swift 3 显示不同的 View Controller [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:15
标题: ios - 基于角色-Firebase、Swift 3 显示不同的 View Controller

我正在构建一个非常简单的应用程序,它将用户的报告发送给管理员。到目前为止,我已经完成了整个前端。我有菜单工作,报告顺序是无缝的。现在是我承担后端的时候了。我是一个新的 Swift 开发人员,完全是自学的(就像你应该是 )我对一些事情感到困惑。我只需要一些指导,我只是通过阅读在后面使用堆栈溢出来寻求建议,但从未问过问题。所以!我要问你,虔诚的堆栈社区,是!:

我有两个用户角色..

  1. 普通用户
  2. 管理员

我希望能够根据他们在 firebase 中的角色,在他们登录时将他们重定向到各自的 View Controller 。现在!我问了我的一个 friend ,他告诉我他们都可以在同一个应用程序中完成,我不需要为管理员制作不同的应用程序。我猜这是真的,因为我相信他的判断。我正在考虑做if检查,即

//If role is = to admin
self.performSegue(admin VC)

//else
self.performSegue(homeScreen)


Best Answer-推荐答案


这将是一个两步过程

第一步是对用户进行身份验证并检索他们的用户 uid

Auth.auth().signIn(withEmail: "[email protected]", password: "password", 
  completion: { (auth, error) in

    if error != nil {
        let err = error?.localizedDescription
        print(err!)
    } else {
        print("succesfully authd")
        let uid = auth!.uid

        assignUserRole(uid)     
    }
})

假设您有一个包含附加用户数据的标准用户节点

users
  uid_0
   fav_food: "pizza"
   user_role: "admin"
  uid_1
   fav_food: "tacos"
   user_role: "normal_user"

第 2 步:随后将调用 assignUserRole 函数以获取用户信息并设置 UI

function assignUserRole(theUid: String) {

   let thisUserRef = appRef.child("users").child(theUid)
   thisUserRef.observeSingleEvent(of: .value, with: { snapshot in
       let userDict = snapshot.value as! [String: Any]
       let favFood = userDict["fav_food"] as! String
       let userRole = userDict["user_role"] as! String
       print("  \(favFood)  \(userRole")

      if userRole == "admin" {
        //display admin viewController
      } else {
        //display normal user viewController
     }
   })
}

关于ios - 基于角色-Firebase、Swift 3 显示不同的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45049666/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4