Swift Firebase Facebook登录异常:可获名称但邮箱返回Nil,数据库未写入
Hey there, let's break down the key areas to troubleshoot your Facebook login issue—since Google sign-in works fine, the problem is almost certainly tied to Facebook-specific permissions, Graph API behavior, or misconfigurations between Firebase and your Facebook app. Here's what to focus on:
- First, double-check that your
self.facebookPermissionsarray includes the"email"permission. If it doesn't, the Graph API will never return a user's email address, no matter what. - Head to your Facebook Developer Dashboard:
- If your app is still in Development Mode, only test users (added in the dashboard) will have their email returned. For regular users, you need to submit the
emailpermission for App Review and get it approved. - Ensure your app's Privacy Policy URL and Terms of Service URL are filled out in the app settings—Facebook sometimes blocks permission access if these are missing, even in development mode.
- If your app is still in Development Mode, only test users (added in the dashboard) will have their email returned. For regular users, you need to submit the
Your code assumes the Graph API will always return an email field, but that's not guaranteed. Add a debug print right after you parse the result to see exactly what data Facebook is sending:
guard let data: [String:AnyObject] = result as? [String:AnyObject] else { print("Can't pull data from JSON") return } print("Graph API Raw Response: \(data)") // Add this line
If the response doesn't include email, it could be because:
- The user's Facebook account doesn't have an email linked to it.
- The user denied the email permission during login.
- Your app doesn't have the correct permissions to access it (see point 1).
- In the Firebase Console, go to Authentication > Sign-in method > Facebook and confirm your App ID and App Secret match exactly what's in the Facebook Developer Dashboard. Even a tiny typo here can break data sync.
- Verify your iOS bundle ID in Firebase Project Settings > General matches the one in Xcode and your Facebook app's settings. Bundle IDs are case-sensitive, so make sure they're identical.
- After signing in with Facebook, print
user?.emailin the Firebase Auth callback. If this isnil, it means Facebook isn't passing the email to Firebase—double-check that your Facebook app is configured to share user email with third-party services.
You have a major timing problem here: you're navigating to the main screen and dismissing the login controller before the Graph API request and database write finish. Since these are asynchronous operations, your code skips the database write entirely if the email guard fails, and you'll never see the error messages because the controller is already gone.
Move your navigation code inside the database write completion block to ensure it only runs after data is saved (or errors are handled):
usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in if err != nil { print(err ?? "Error saving user to database") // Show an alert to the user here return } // Only navigate after successful database write self.dismiss(animated: true, completion: { if let viewController = self.storyboard?.instantiateViewController(withIdentifier: "Customer Profile") { UIApplication.shared.keyWindow?.rootViewController = viewController } }) })
Also, add fallback logic for when the email field is missing—don't just return silently. For example, show an alert asking the user to enter their email manually, or proceed without it if your app allows that.
- Make sure your
FBSDKLoginKitversion is compatible with your Firebase SDK version. Mismatched versions can cause unexpected permission or data sync issues. - Check your
Info.plistto ensure these entries are present and correct:FacebookAppID(matches your Facebook app ID)FacebookDisplayName(matches your app's display name in Facebook settings)LSApplicationQueriesSchemeswithfbauth2as an item
Start with the Graph API debug print—it'll tell you exactly what data you're getting, which is the fastest way to narrow down the root cause. Then fix the async logic so you can properly handle errors and confirm when data is saved.
内容的提问来源于stack exchange,提问作者Lukas Bimba

