Firebase认证当前用户邮箱无法显示至Label问题求助
Let’s break down why your emailLabel might not be showing the user’s email and how to fix it:
Check if a user is actually logged in
The most common culprit here is thatAuth.auth().currentUserisnil. Maybe the user session expired, or you’re testing this view before the user has signed in. Add a quick print statement to confirm this:override func viewDidLoad() { super.viewDidLoad() let userEmail = Auth.auth().currentUser?.email ?? "No user logged in" print(userEmail) self.emailLabel.text = userEmail }If the print output reads "No user logged in", you’ll need to make sure your authentication flow properly handles user sign-in before this view loads.
Verify your IBOutlet connection
It’s easy for Storyboard/XIB connections to break accidentally. Right-click onemailLabelin your code—if there’s no blue line linking it to the label in your interface builder, re-drag the IBOutlet from the label in your storyboard to your view controller code to re-establish the connection.Check the label’s visual properties
Sometimes the label is working, but you can’t see it:- Make sure the
textColorisn’t identical to the label’s background color - Confirm
isHiddenis set tofalse - Double-check that the label’s frame is visible on the screen (not off-screen or covered by another view)
- Ensure
numberOfLinesis set to at least 1 (or 0 if you want wrapping, though emails are usually single-line)
- Make sure the
Use auth state listening for better reliability
viewDidLoadmight run before the auth system fully initializes the current user session. Instead, add a listener to update the label automatically whenever the user’s auth state changes:override func viewDidLoad() { super.viewDidLoad() // Listen for auth state changes to keep the label updated Auth.auth().addStateDidChangeListener { [weak self] _, user in guard let self = self else { return } self.emailLabel.text = user?.email ?? "Not logged in" } }Using
[weak self]prevents memory leaks, and this setup will refresh the label instantly when the user logs in or out.
内容的提问来源于stack exchange,提问作者terry bridges

