斯威夫特:我必须调用 Facebook 登录按钮两次才能获得 FBSDKAccessToken
Posted
技术标签:
【中文标题】斯威夫特:我必须调用 Facebook 登录按钮两次才能获得 FBSDKAccessToken【英文标题】:Swift: I have to call Facebook loginbutton twice to get an FBSDKAccessToken 【发布时间】:2016-04-30 09:53:14 【问题描述】:我的问题是:我必须按两次 Facebook 登录按钮才能获得 FBSDKAccessToken。之后,令牌在应用程序的整个使用过程中都是持久的。在第一次按下按钮时,AccessToken 似乎在那里,并且在观察者被调用四次后再次“消失”。
当我将观察者移动到我的 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...")
)
【问题讨论】:
【参考方案1】:在 viewDidLoad() 中,我询问了视图用户是否已登录。使用 Facebook 登录和使用 Firebase 进行身份验证时,有几个异步函数。结果尚未正确处理,因此 viewDidLoad 很快就会调用。
在登录函数本身加载完成后,我删除了对结果的处理,不再在 viewDidLoad() 中
【讨论】:
以上是关于斯威夫特:我必须调用 Facebook 登录按钮两次才能获得 FBSDKAccessToken的主要内容,如果未能解决你的问题,请参考以下文章