gpt4 book ai didi

ios - 如何使用 Swift 删除注销按钮并永久存储 session FBSDK IOS

转载 作者:可可西里 更新时间:2023-11-01 01:35:55 24 4
gpt4 key购买 nike

我正在创建一个应用程序,用户可以在其中使用 Facebook 登录。我正在获取用户信息,例如个人资料图片用户名等。现在,当我点击使用 facebook 按钮登录时,FB 请求允许我登录。但是随后在与 LoginWithFacebook 按钮相同的位置显示了注销按钮。我想删除注销按钮,因为我不希望用户注销。我想存储 session 并一直使用它直到应用程序被卸载,因为我不希望用户每次打开应用程序时都登录。 到目前为止,这是我的代码

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()
}

最佳答案

您可以在通过 facebook 登录后 Conceal 注销按钮,方法如下,

 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()
}
}
})
}
}

希望对您有所帮助。

关于ios - 如何使用 Swift 删除注销按钮并永久存储 session FBSDK IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37341398/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com