2016-04-30 72 views
0

我的問題是:我必須按兩次Facebook登錄按鈕才能獲得FBSDKAccessToken。之後,令牌在整個應用程序的使用過程中保持不變。在按下第一個按鈕時,AccessToken似乎在那裏,並且在觀察者被召喚四次之後再次消失。Swift:我必須撥打Facebook登錄按鈕兩次才能獲得FBSDKAccessToken

當我將觀察者移動到viewDidAppear()並且取消註釋'FBSDKProfile.enableUpdatesOnAccessTokenChange(true)'行時,也會發生同樣的情況。

我有以下代碼: 在我的AppDelegate:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    // Let Facebook API know we finished launching   
    FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions) 
    return true 
} 

func applicationDidBecomeActive(application: UIApplication) { 
    // Register actions, such as add clicks and app FB statistics 
    FBSDKAppEvents.activateApp() 
} 

func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool { 
    return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation) 
} 

在我LoginOrSignupViewController

class LoginOrSignupViewController: UIViewController, FBSDKLoginButtonDelegate { 

@IBOutlet weak var buttonFacebooklogin: FBSDKLoginButton! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Customize the facebookLogin button 
    buttonFacebooklogin.readPermissions = ["public_profile", "email", "user_friends"] 
    buttonFacebooklogin.delegate = self 
    //FBSDKProfile.enableUpdatesOnAccessTokenChange(true) 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(listenForFBLogin), name: FBSDKAccessTokenDidChangeNotification, object: nil) 
} 

在我viewDidAppear()我有一些打印語句。以及在我的Facebook登錄按鈕。我注意到Observer函數被調用了4次,前兩次有一個AccessToken,最後兩次沒有。

當我再次按下Facebook登錄按鈕時,它的行爲就像它應該。 打印語句:

我第一次按下登錄鍵:

Observer listenForFBLogin function... 
We got a token here... 
<FBSDKAccessToken: 0x7fa0896b5f40> 
Observer listenForFBLogin function... 
We got a token here... 
<FBSDKAccessToken: 0x7fa0896b5f40> 
Observer listenForFBLogin function... 
Even in the listenForFBLogin the Accestoken is empty... 
Observer listenForFBLogin function... 
Even in the listenForFBLogin the Accestoken is empty... 
User is not logged in... 

我第二次按下登錄鍵:

Observer listenForFBLogin function... 
We got a token here... 
<FBSDKAccessToken: 0x7fa08b8407b0> 
Observer listenForFBLogin function... 
We got a token here... 
<FBSDKAccessToken: 0x7fa08b8407b0> 
Tickee isUserLoggedIn: Logged in with Facebook and Firebase... 
User is logged in, go to the tabbar... 

我的Facebook登錄代碼

func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) { 

    if error != nil { 
     // Error login 
     NSLog("Error van loginButton func:") 
     NSLog(error.localizedDescription) 
    } else if result.isCancelled { 
     // User cancelled login 
     // NSLog("User cancelled Facebook login") 
    } else { 

     // User logged in. Proceed to request Auth from Firebase 

     // Signup or Login with Firebase 
     let firebaseRef = Firebase(url: "https://tickee.firebaseio.com/") 
     let accessToken = FBSDKAccessToken.currentAccessToken().tokenString 

     // Auth the User in Firebase 
     firebaseRef.authWithOAuthProvider("facebook", token: accessToken, withCompletionBlock: { 
      (error, authData) in 

      if (error != nil) { 
       NSLog("Login failed") 
      } else { 

       // Check if UID is saved to UserDefaults 
       let userUID = self.tickee.getUserUIDFromUserDefaults() 

       // Check if UID exists 
       if userUID == nil { 

        // UID doesn't not exists, there is no user 
        print("User doesn't exist yet, create new Facebook user in Firebase...") 

        // Create User in Firebase 
        self.tickee.createUserInFirebase("facebook", nameField: nil) 

        // Save UID in NSUserDefaults 
        self.tickee.saveUserUIDtoNSUserDefaults() 
       } else { 
        print("User exists already, no need to create one again...") 
       } 

       print("Done loggin in, now wait for the observer...") 

      } 
     }) 
    } 
} 

回答

0

在viewDidLoad()中,我詢問用戶是否已登錄。當使用Facebook登錄並使用Firebase進行Authing時,有幾個異步函數。結果尚未正確處理,所以viewDidLoad很快就會調用。

我在登錄功能本身加載完成後不再處理結果,並且不再處於viewDidLoad()