You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

1. Verify Facebook App Permissions & Login Scopes
  • First, double-check that your self.facebookPermissions array 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 email permission 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.
2. Debug the Graph API Response

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).
3. Check Firebase-Facebook Integration Setup
  • 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?.email in the Firebase Auth callback. If this is nil, 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.
4. Fix Critical Async Logic Issues in Your Code

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.

5. Confirm SDK Compatibility & Info.plist Config
  • Make sure your FBSDKLoginKit version is compatible with your Firebase SDK version. Mismatched versions can cause unexpected permission or data sync issues.
  • Check your Info.plist to ensure these entries are present and correct:
    • FacebookAppID (matches your Facebook app ID)
    • FacebookDisplayName (matches your app's display name in Facebook settings)
    • LSApplicationQueriesSchemes with fbauth2 as 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:56:02