我正在创建一个用户可以使用Facebook登录的应用。我正在获取用户信息,如个人资料图片用户名等。现在,当我点击登录与Facebook按钮FB请求允许我进入。但然后有登出按钮显示在LoginWithFacebook按钮是在同一个地方。我想删除注销按钮,因为我不希望用户注销。我想存储会话并一直使用它直到应用程序未被禁止,因为我不希望用户每次打开应用程序时都登录。 到目前为止,这是我的代码
class ViewController: UIViewController, FBSDKLoginButtonDelegate {
@IBOutlet weak var btnFacebook: FBSDKLoginButton!
@IBOutlet weak var ivUserProfileImage: UIImageView!
@IBOutlet weak var lblName: UILabel!
var strFirstName: String!
var strLastName: String!
var strPictureURL: String!
override func viewDidLoad() {
super.viewDidLoad()
configureFacebook()
}
func configureFacebook()
{
btnFacebook.readPermissions = ["public_profile", "email", "user_friends"];
btnFacebook.delegate = self
}
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)
{
FBSDKGraphRequest.init(graphPath: "me", parameters: ["fields":"first_name, last_name, picture.type(large)"]).startWithCompletionHandler { (connection, result, error) -> Void in
//fetching first and last name
self.strFirstName = (result.objectForKey("first_name") as? String)!
self.strLastName = (result.objectForKey("last_name") as? String)!
self.strPictureURL = (result.objectForKey("picture")?.objectForKey("data")?.objectForKey("url") as? String)!
// contactinating first and last name
self.lblName.text = "Welcome, \(self.strFirstName) \(self.strLastName)"
// assinging image
self.ivUserProfileImage.image = UIImage(data: NSData(contentsOfURL: NSURL(string: self.strPictureURL)!)!)
}
}
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
let loginManager: FBSDKLoginManager = FBSDKLoginManager()
//for logging out from facebook
loginManager.logOut()
}
答案 0 :(得分:1)
手动将fb登录按钮变量添加到视图控制器
view.addSubView(fbLoginBtn)
在viewDidLoad中
FBSDKAccessToken.currentAccessToken()
通过支票FBSDKLoginManager().logOut()
处理隐藏的登录状态btn是否有值
您可以按{{1}}
以编程方式注销答案 1 :(得分:1)
您可以使用以下代码(例如
)通过Facebook登录后隐藏退出按钮 if(FBSDKAccessToken.currentAccessToken() != nil)
{
self.btnFacebook.hidden = true
}
else
{
self.btnFacebook.hidden = false
}
注意:您需要在每次登录后注册并在代码中注销,以便您可以轻松维护该按钮日志。
对于Session,在LoginController的viewDidLoad中写这个以获取用户信息,
self.btnFacebook.delegate = self
self.btnFacebook.readPermissions = ["public_profile","email","user_friends"]
FBSDKProfile.enableUpdatesOnAccessTokenChange(true)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "getUserProfile:", name: FBSDKProfileDidChangeNotification, object: nil)
if(FBSDKAccessToken.currentAccessToken() != nil)
{
let accessToken = FBSDKAccessToken.currentAccessToken()
if(accessToken != nil)
{
let req = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email,name"], tokenString: accessToken.tokenString, version: nil, HTTPMethod: "GET")
req.startWithCompletionHandler({ (connection, result, error : NSError!) -> Void in
if(error == nil)
{
let userInfo = result as AnyObject
if let _ = userInfo.valueForKey("email") as? NSString
{
// Extract user info here
let userDic : NSDictionary = NSDictionary(dictionary: userInfo as! [NSObject : AnyObject])
print(userDic)
}
else
{
// Logout if you want or display error message
// let loginManager = FBSDKLoginManager()
// loginManager.logOut()
}
}
})
}
}
调用此观察者方法,
func getUserProfile(notification : NSNotification)
{
let accessToken = FBSDKAccessToken.currentAccessToken()
if(accessToken != nil)
{
let req = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"email,name"], tokenString: accessToken.tokenString, version: nil, HTTPMethod: "GET")
req.startWithCompletionHandler({ (connection, result, error : NSError!) -> Void in
if(error == nil)
{
let userInfo = result as AnyObject
if let _ = userInfo.valueForKey("email") as? NSString
{
let userDic : NSDictionary = NSDictionary(dictionary: userInfo as! [NSObject : AnyObject])
print(userDic)
}
else
{
// logout or error message
//let loginManager = FBSDKLoginManager()
//loginManager.logOut()
}
}
})
}
}
希望这会对你有所帮助。