菜鸟教程小白 发表于 2022-12-11 18:15:13

ios - 基于角色-Firebase、Swift 3 显示不同的 View Controller


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

<p>我有两个用户角色..</p>

<ol>
<li>普通用户</li>
<li>管理员</li>
</ol>

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

<p></p><div class="snippet"data-lang="js"data-hide="false"data-console="true"data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>//If role is = to admin
self.performSegue(admin VC)

//else
self.performSegue(homeScreen)</code></pre>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这将是一个两步过程</p>

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

<pre><code>Auth.auth().signIn(withEmail: &#34;[email protected]&#34;, password: &#34;password&#34;,
completion: { (auth, error) in

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

      assignUserRole(uid)   
    }
})
</code></pre>

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

<pre><code>users
uid_0
   fav_food: &#34;pizza&#34;
   user_role: &#34;admin&#34;
uid_1
   fav_food: &#34;tacos&#34;
   user_role: &#34;normal_user&#34;
</code></pre>

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

<pre><code>function assignUserRole(theUid: String) {

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

      if userRole == &#34;admin&#34; {
      //display admin viewController
      } else {
      //display normal user viewController
   }
   })
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 基于角色-Firebase、Swift 3 显示不同的 ViewController ,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/45049666/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/45049666/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 基于角色-Firebase、Swift 3 显示不同的 View Controller