[英]Facebook IOS SDK : how to do silent sign in?
I have an app doing a facebook login, which works well, but everytime I reopen it I have to connect again to facebook to do the sign-in. 我有一个执行Facebook登录的应用程序,该应用程序运行良好,但是每次重新打开它时,我都必须再次连接至Facebook才能登录。 I'm also using google sign-in sdk where I can call the function gSignIn.signInSilently()
, is there something similar for facebook? 我也在使用Google登录sdk,可以在其中调用函数gSignIn.signInSilently()
,facebook也有类似的东西吗? I found this for the javascript sdk but I don't know if it's possible for the ios SDK and how to use it in swift... 我为javascript sdk找到了这个 ,但我不知道ios SDK是否可行以及如何快速使用它...
The Facebook SDK automatically maintains the login state, which can be confirmed by checking for the access token. Facebook SDK自动维护登录状态,可以通过检查访问令牌来确认登录状态。
You can check for the access using the following method: 您可以使用以下方法检查访问权限:
FBSDKAccessToken.currentAccessToken()
You can check for the presence of the token which would mean that the user is logged in. 您可以检查令牌的存在,这意味着用户已登录。
i've tried this 我试过了
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
}
I am saving the access token string and manually setting it on consecutive launches to bypass the re-login flow. 我正在保存访问令牌字符串,并在连续启动时手动设置它以绕过重新登录流程。
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)
}
}
}
NOTE: For the ease of this example, the Facebook Access Token string is being saved to UserDefaults
but ideally it should be saved to the Keychain. 注意:为方便UserDefaults
,将Facebook访问令牌字符串保存到UserDefaults
但理想情况下应将其保存到钥匙串。
(Swift 4 / Facebook SDK 4.30) (Swift 4 / Facebook SDK 4.30)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.