[英]Facebook SDK and Swift 2.0 - Problems with handling accessToken
my name is Vincent and I need your help ! 我叫Vincent,需要您的帮助!
(My code is not exhaustive) No problem to login or logout with the Facebook SDK ... BUT When I start my app, no access token and when i click on the Login button, it prints me an accessToken, then I logout. (我的代码并不详尽)使用Facebook SDK登录或注销都没有问题...但是,当我启动我的应用程序时,没有访问令牌,并且当我单击“登录”按钮时,它会向我打印一个accessToken,然后注销。
How to get the accessToken when the app starts ? 应用启动时如何获取accessToken?
import UIKit
import FBSDKCoreKit
import FBSDKLoginKit
class HomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
getUserStatuts()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK -> Facebook Connect
func loginToFacebook() {
if let fbToken = FBSDKAccessToken.currentAccessToken() {
print("Acces token : \(fbToken)")
loginManager.logOut()
print("Logout")
} else {
print("Login")
loginManager.logInWithPublishPermissions(publishPermissions, fromViewController: nil, handler: {
(result:FBSDKLoginManagerLoginResult!, error:NSError!) -> Void in
if error == nil {
self.getUserData()
} else {
print(error.localizedDescription)
}
})
}
}
func getUserData() {
let readPermissions = ["public_profile", "email", "user_friends", "user_birthday"]
let parameters = ["fields": "id, first_name, last_name, name, birthday, picture.type(large)"]
let request = FBSDKGraphRequest(graphPath: "me", parameters: parameters)
request.startWithCompletionHandler({
(connection, result, error) -> Void in
if error == nil {
// display info
}
})
}
In the login function you want to implement the let fbLogin = FBSDKLoginManager() fblogin.logout()
在登录功能中,您要实现let fbLogin = FBSDKLoginManager() fblogin.logout()
the fblogin.logout()
must be implemented before fbLogin.logInWithReadPermissions(["email"], fromViewController: self) { (facebookResult, facebookError) in if facebookError != nil {
` to clear any access tokens from previous login. 必须在fbLogin.logInWithReadPermissions(["email"], fromViewController: self) { (facebookResult, facebookError) in if facebookError != nil {
`之前执行fblogin.logout()
fbLogin.logInWithReadPermissions(["email"], fromViewController: self) { (facebookResult, facebookError) in if facebookError != nil {
以清除先前登录中的所有访问令牌。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.