Facebook IOS SDK:如何进行静默登录?
Facebook IOS SDK : how to do silent sign in?
我有一个用于 facebook 登录的应用程序,效果很好,但每次我重新打开它时,我都必须再次连接到 facebook 才能进行登录。我也在使用 google 登录 sdk,我可以在其中调用函数 gSignIn.signInSilently()
,facebook 有类似的东西吗?我找到 javascript SDK 的 this 但我不知道 ios SDK 是否可行以及如何在 swift...[=13= 中使用它]
Facebook SDK 自动维护登录状态,可以通过检查访问令牌来确认。
您可以使用以下方法检查访问权限:
FBSDKAccessToken.currentAccessToken()
您可以检查是否存在表示用户已登录的令牌。
查看 docs 了解更多详情。
我试过了
if(![FBSDKAccessToken currentAccessToken])
{
FBSDKLoginManager *manager = [[FBSDKLoginManager alloc]init];
[manager logInWithReadPermissions:@[@"public_profile", @"email",@"user_photos"] handler:^(FBSDKLoginManagerLoginResult *result,NSError *error)
{
if(error == nil)
{
NSLog(@"Facebook - successfully login %@",result.token);
//login successfully
//do your stuff here
}
}];
}
else
{
//already login
//Do Your stuff here
}
我正在保存访问令牌字符串并在连续启动时手动设置它以绕过重新登录流程。
func loginWithFacebook() {
//Check for previous Access Token
if let accessToken = AccessToken.current {
//AccessToken was obtained during same session
getAccountDetails(withAccessToken: accessToken)
}
else if let strAuthenticationToken = UserDefaults.standard.string(forKey: "AccessToken_Facebook") {
//A previous access token string was saved so create the required AccessToken object
let accessToken = AccessToken(authenticationToken: strAuthenticationToken)
//Skip Login and directly proceed to get facebook profile data with an AccessToken
getAccountDetails(withAccessToken: accessToken)
}
else {
//Access Token was not available so do the normal login flow to obtain the Access Token
LoginManager().logIn(readPermissions: [.publicProfile, .email], viewController: nil) { loginResult in
switch loginResult {
case .failed(let error):
print(error)
case .cancelled:
print("User cancelled login.")
case .success(let grantedPermissions,
let declinedPermissions,
let accessToken):
//Save Access Token string for silent login purpose later
let strAuthenticationToken = accessToken.authenticationToken
UserDefaults.standard.set(strAuthenticationToken,
forKey: "AccessToken_Facebook")
//Proceed to get facebook profile data
self.getAccountDetails(withAccessToken: accessToken)
}
}
}
}
func getAccountDetails(withAccessToken accessToken: AccessToken) {
let graphRequest: GraphRequest = GraphRequest(graphPath : "me",
parameters : ["fields" : "id, name, email"],
accessToken : accessToken,
httpMethod : GraphRequestHTTPMethod.GET,
apiVersion : GraphAPIVersion.defaultVersion)
graphRequest.start { (response, result) in
switch result {
case .success(let resultResponse):
print(resultResponse)
case .failed(let error):
print(error)
}
}
}
注意:为方便此示例,Facebook 访问令牌字符串被保存到 UserDefaults
,但理想情况下应将其保存到钥匙串。
(Swift 4 / Facebook SDK 4.30)
我有一个用于 facebook 登录的应用程序,效果很好,但每次我重新打开它时,我都必须再次连接到 facebook 才能进行登录。我也在使用 google 登录 sdk,我可以在其中调用函数 gSignIn.signInSilently()
,facebook 有类似的东西吗?我找到 javascript SDK 的 this 但我不知道 ios SDK 是否可行以及如何在 swift...[=13= 中使用它]
Facebook SDK 自动维护登录状态,可以通过检查访问令牌来确认。
您可以使用以下方法检查访问权限:
FBSDKAccessToken.currentAccessToken()
您可以检查是否存在表示用户已登录的令牌。
查看 docs 了解更多详情。
我试过了
if(![FBSDKAccessToken currentAccessToken])
{
FBSDKLoginManager *manager = [[FBSDKLoginManager alloc]init];
[manager logInWithReadPermissions:@[@"public_profile", @"email",@"user_photos"] handler:^(FBSDKLoginManagerLoginResult *result,NSError *error)
{
if(error == nil)
{
NSLog(@"Facebook - successfully login %@",result.token);
//login successfully
//do your stuff here
}
}];
}
else
{
//already login
//Do Your stuff here
}
我正在保存访问令牌字符串并在连续启动时手动设置它以绕过重新登录流程。
func loginWithFacebook() {
//Check for previous Access Token
if let accessToken = AccessToken.current {
//AccessToken was obtained during same session
getAccountDetails(withAccessToken: accessToken)
}
else if let strAuthenticationToken = UserDefaults.standard.string(forKey: "AccessToken_Facebook") {
//A previous access token string was saved so create the required AccessToken object
let accessToken = AccessToken(authenticationToken: strAuthenticationToken)
//Skip Login and directly proceed to get facebook profile data with an AccessToken
getAccountDetails(withAccessToken: accessToken)
}
else {
//Access Token was not available so do the normal login flow to obtain the Access Token
LoginManager().logIn(readPermissions: [.publicProfile, .email], viewController: nil) { loginResult in
switch loginResult {
case .failed(let error):
print(error)
case .cancelled:
print("User cancelled login.")
case .success(let grantedPermissions,
let declinedPermissions,
let accessToken):
//Save Access Token string for silent login purpose later
let strAuthenticationToken = accessToken.authenticationToken
UserDefaults.standard.set(strAuthenticationToken,
forKey: "AccessToken_Facebook")
//Proceed to get facebook profile data
self.getAccountDetails(withAccessToken: accessToken)
}
}
}
}
func getAccountDetails(withAccessToken accessToken: AccessToken) {
let graphRequest: GraphRequest = GraphRequest(graphPath : "me",
parameters : ["fields" : "id, name, email"],
accessToken : accessToken,
httpMethod : GraphRequestHTTPMethod.GET,
apiVersion : GraphAPIVersion.defaultVersion)
graphRequest.start { (response, result) in
switch result {
case .success(let resultResponse):
print(resultResponse)
case .failed(let error):
print(error)
}
}
}
注意:为方便此示例,Facebook 访问令牌字符串被保存到 UserDefaults
,但理想情况下应将其保存到钥匙串。
(Swift 4 / Facebook SDK 4.30)