2017-02-13 73 views
3

我是一名自學新手,正在開發使用swift的iPhone應用程序。我正在設計一個使用FBSDK進行Facebook身份驗證的登錄頁面。我已經成功實現了登錄和註銷登錄按鈕。它返回用戶名和ID,但它無法圖表用戶電子郵件ID,並顯示代碼2500錯誤。此外,我無法使用新用戶登錄。它會自動使用同一個用戶登錄。如何在Swift iOS for iPhone中使用FBSDK獲取用戶的電子郵件地址?

以下是錯誤:

UserInfo={com.facebook.sdk:FBSDKGraphRequestErrorCategoryKey=0, com.facebook.sdk:FBSDKGraphRequestErrorHTTPStatusCodeKey=400, com.facebook.sdk:FBSDKErrorDeveloperMessageKey=An active access token must be used to query information about the current user., com.facebook.sdk:FBSDKGraphRequestErrorGraphErrorCode=2500, com.facebook.sdk:FBSDKGraphRequestErrorParsedJSONResponseKey={ 
    body =  { 
     error =   { 
      code = 2500; 
      "fbtrace_id" = BimF4Nuqx0v; 
      message = "An active access token must be used to query information about the current user."; 
      type = OAuthException; 
     }; 
    }; 
    code = 400; 
}}) 
Successfully logged in with Facebook... 
123 
result Optional({ 
    id = 120008981838163; 
    name = "Nisha Ios"; 
}) 
Did Logout of Facebook 

這是我在AppDelegate.swift代碼:

import UIKit 
import CoreData 
import FBSDKCoreKit 


@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate, { 

    var window: UIWindow? 

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     // Override point for customization after application launch. 

     FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions) 

     return true 
    } 


    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool { 

     var handled=FBSDKApplicationDelegate.sharedInstance().application(app, open: url, 
                      sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String!, annotation: options[UIApplicationOpenURLOptionsKey.annotation]) 

return handled 
} 

    func applicationWillResignActive(_ application: UIApplication) { 
     // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
     // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 
     FBSDKAppEvents.activateApp() 
    } 

這是我在ViewController.swift代碼:

import UIKit 
import FBSDKLoginKit 
import FBSDKCoreKit 


class ViewController: UIViewController, UITextFieldDelegate, FBSDKLoginButtonDelegate, { 


    @IBOutlet var emailField: UITextField! 

    @IBOutlet var passField: UITextField! 

    @IBOutlet var customButton: FBSDKLoginButton! 


    @IBOutlet var myView: UIView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.emailField.delegate = self; 
     self.passField.delegate = self; 

     customButton.readPermissions=["email","public_profile"] 
     customButton.delegate=self 

     customButton.addTarget(self, action: #selector(handleCustomFBLogin), for: UIControlEvents.touchUpInside) 

func handleCustomFBLogin(){ 

    // FBSDKLoginManager().logIn(withReadPermissions: ["email","public_profile"], from: self) 
    // { (result, error) -> Void in 
    // if error != nil { 
     // print("custom FB Login Failed",error) 
     //  } 
    //} 
       self.showEmail() 
       print(123) 
     } 


    func showEmail() 
    { 
     FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "email, id, name"]).start { 
      (connection, result, err) in 
      if(err == nil) 
      { 
       print("result \(result)") 
      } 
      else 
      { 
       print("error \(err)") 
      } 
     } 

    } 

    public func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) { 

     if error != nil 
     { 
      print(error) 
      return 

     } 
     else{ 
      print("Successfully logged in with Facebook... ") 

     } 
     //showEmail() 
    } 

    func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!){ 

     print("Did Logout of Facebook") 

    } 

在這裏函數handleCustomFBLogin(),我已經評論了一些代碼。如果我取消註釋他們,我無法獲得用戶名或ID,只能得到代碼2500錯誤。

有人能告訴我我做錯了什麼嗎?還幫我用新用戶登錄,而不是用同一個用戶自動登錄? 非常感謝您提前致謝:-)

+1

http://stackoverflow.com/questions/9347104/register-with-facebook-sometimes-doesnt-provide-email/18377407#18377407 –

回答

3

我正在使用它,它對我的​​工作很好。請確保您使用的帳戶具有公開的電子郵件和其他詳細信息。您只能獲得Facebook用戶的公開詳細信息。其次,你的代幣應該是有效的。

func getFBUserData() 
    { 

      showIndicator() 

      if((FBSDKAccessToken.currentAccessToken()) != nil 

      { 

       FBSDKGraphRequest(graphPath: "me", 

            parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email , gender"]).startWithCompletionHandler(

            { (connection, result, error) -> Void in 

             self.hideIndicator() 

        if (error == nil) 

        { 

         self.objSocialInfo = SignupUserStructure() 

         self.objSocialInfo.Name = String(result.objectForKey("first_name")!) + " " + String(result.objectForKey("last_name")!) 

         self.objSocialInfo.Email = String(result.objectForKey("email")!) 

         self.objSocialInfo.ProfilePicUrl = (result.objectForKey("picture")?.objectForKey("data")?.objectForKey("url") as? String)! 

         self.objSocialInfo.SocialId = String(result.objectForKey("id")!) 

         self.objSocialInfo.Gender = String(result.objectForKey("gender")!) 

        }}) 
      } 
    } 
+0

太謝謝你了。我的電子郵件未被驗證。這就是爲什麼它不會返回用戶電子郵件。當我添加FBSDKAccessToken條件時,該錯誤已被清除。再次感謝。 – Nisha

+0

歡迎@Nisha快樂的幫助。你可以接受我的回答。 – ChanWarde

+0

:-)我做到了。謝謝 – Nisha

相關問題