【问题标题】:displaying user email on viewcontroller gives optional"email adress" [duplicate]在视图控制器上显示用户电子邮件提供可选的“电子邮件地址”[重复]
【发布时间】:2021-09-05 08:42:40
【问题描述】:

所以我在我的 ios 应用程序中使用 firebase 身份验证,我想在视图控制器上的 UIlabels 中显示电子邮件地址和用户名。但是当我在 UIlabel 上显示 Auth.auth().email 的值时,标签会显示 Optional"email adress"。我如何摆脱 Optional 以及如何允许用户在 firebase 身份验证中拥有显示名称?

import Firebase
import FirebaseAuth

class ProfileViewController: UIViewController {
    @IBOutlet weak var profiepic: UIImageView!
    @IBOutlet weak var UsernameLabel: UILabel!
    @IBOutlet weak var EmailLabel: UILabel!
    @IBOutlet weak var league: UILabel!
    @IBOutlet weak var Achievements: UIButton!
    @IBOutlet weak var resetpasswd: UIButton!
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        UsernameLabel.layer.borderColor = UIColor.black.cgColor
        EmailLabel.layer.borderColor = UIColor.black.cgColor
        league.layer.borderColor = UIColor.black.cgColor
        Achievements.layer.cornerRadius = 55/2
        resetpasswd.layer.cornerRadius = 55/2
        resetpasswd.layer.borderColor = UIColor.black.cgColor
        displayinfo()
    }
    func displayinfo() {
        let user = Auth.auth().currentUser
        if let user = user {
          // The user's ID, unique to the Firebase project.
          // Do NOT use this value to authenticate with your backend server,
          // if you have one. Use getTokenWithCompletion:completion: instead.
          let email = user.email
          let photoURL = user.photoURL
            EmailLabel.text = "Email: \(email)"
          // ...
        }
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

【问题讨论】:

标签: swift firebase


【解决方案1】:

您需要使用ifguard 才能正确显示字符串信息。

使用if

func displayinfo() {
     let user = Auth.auth().currentUser
     if let user = user {
          if let email = user.email {
               EmailLabel.text = "Email: \(email)"
          }

          if let photoURL = user.photoURL {
             ...
          }
          // ...
    }
}

使用guard

func displayinfo() {
     guard let user = Auth.auth().currentUser else {
         print("No user info found")
         return
     }

     if let email = user.email {
         EmailLabel.text = "Email: \(email)"
         //EmailLabel.text = "Email: " + email
     }

     if let photoURL = user.photoURL {
         ...
     }
     // ...
}

如果您对这些解决方案有任何问题,请告诉我。

除此之外,我宁愿这样写 UIViewController 似乎更清晰的方式。

class ProfileViewController: UIViewController {

    @IBOutlet weak var profiepic: UIImageView!
    @IBOutlet weak var lblUsername: UILabel! {
        didSet {
            lblUsername.layer.borderColor = UIColor.black.cgColor
        }
    }

    @IBOutlet weak var lblEmail: UILabel! {
        didSet {
            lblEmail.layer.borderColor = UIColor.black.cgColor
        }
    }

    @IBOutlet weak var lblLeague: UILabel! {
        didSet {
            lblLeague.layer.borderColor = UIColor.black.cgColor
        }
    }

    @IBOutlet weak var btnAchievements: UIButton! {
        didSet {
            btnAchievements.layer.cornerRadius = 55/2
            // For button height, instead of 55 here you can use, btnAchievements.bounds.height / 2 or use constrain also to change button height when bound changes
        }
    }

    @IBOutlet weak var btnReset: UIButton! {
        didSet {
            btnReset.layer.cornerRadius = 55/2
            btnReset.layer.borderColor = UIColor.black.cgColor
        }
    }

    private var currentUser: AuthUser? {// Type of Auth.auth().currentUser
        didSet {
            // Use above code for displayInfo or simply call displayInfo from here
        }
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        self.currentUser = Auth.auth().currentUser
    }
    
    ...
}

我希望这也能帮助您设计其他UIViewControllers。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 2016-03-05
    相关资源
    最近更新 更多