以编程方式从 Facebook 注销 iOS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29623191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:36:47  来源:igfitidea点击:

Logout from Facebook programmatically iOS

iosobjective-cfacebookfacebook-sdk-4.0

提问by Ahd Radwan

I am trying to logout from Facebook programmatically without using FBSDKLoginButtoni had search how could I do i found this answer Can we logout facebook programaticallybut the problem is the FBSessionis deprecated in new iOS FBSDK version

我正在尝试以编程方式从 Facebook 注销而不使用FBSDKLoginButton我搜索过我该怎么做我找到了这个答案我们可以以编程方式注销 Facebook但问题是FBSession在新的 iOS FBSDK 版本中已弃用

my question is Is there any way to clear the fb session in the new iOS FBSDK version? if there any way to logout from Facebook programmatically? or how could I call the logout action from FBSDKLoginButton

我的问题是有什么方法可以清除新 iOS FBSDK 版本中的 fb 会话?是否有任何方法以编程方式从 Facebook 注销?或者我怎么能从FBSDKLoginButton

Thanking in advance :)

提前致谢:)

回答by mars

You have two methods to logout. First, as suggested by Inder Kumar Rathore

您有两种注销方法。首先,正如 Inder Kumar Rathore 所建议的

FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
[loginManager logOut];

Second is by setting the currentAccessToken to nil

其次是通过将 currentAccessToken 设置为 nil

[FBSDKAccessToken setCurrentAccessToken:nil];

@cookiemonsta hope second method works for you.

@cookiemonsta 希望第二种方法适合你。

回答by Inder Kumar Rathore

FBSDKLoginManageris your need, it has logOutmethod but you might have to use your custom login

FBSDKLoginManager是您的需要,它有logOut方法,但您可能必须使用自定义登录

e.g.

例如

FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
[loginManager logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
  if (error) {
    // Process error
  } else if (result.isCancelled) {
    // Handle cancellations
  } else {
    // If you ask for multiple permissions at once, you
    // should check if specific permissions missing
    if ([result.grantedPermissions containsObject:@"email"]) {
      // Do work
    }
  }
}];

//then logout
[loginManager logOut];

回答by Cmar

Swift version:

迅捷版:

FBSDKLoginManager().logOut()

You can use FBSDKLoginManagereven if you logged in with FBSDKLoginButton.

您可以使用FBSDKLoginManager,即使您登录时FBSDKLoginButton

回答by Chetan

For Swift 3 and 4

对于 Swift 3 和 4

I would like to use the code mentioned over here, How to logout user using Facebook authentication using Swift and iOS?

我想使用这里提到的代码, 如何使用 Swift 和 iOS 使用 Facebook 身份验证注销用户?

where HardikDG mentioned a good answer for logout. what you need to do is add below line before the login happen,

HardikDG 在那里提到了一个很好的注销答案。您需要做的是在登录之前添加以下行,

fbLoginManager.loginBehavior = FBSDKLoginBehavior.web

and while logout use below code

并在注销时使用下面的代码

FBSDKAccessToken.setCurrent(nil)
FBSDKProfile.setCurrent(nil)
FBSDKLoginManager().logOut()

this works perfectly for me.

这对我来说非常有效。

回答by DevB2F

Swift 3 and Swift 4:

斯威夫特 3 和斯威夫特 4:

import FacebookLogin
import FacebookCore

let loginManager = LoginManager()
loginManager.logOut()