使用 Facebook ios SDK 在应用程序中注销 Facebook
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15610152/
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
Logout of Facebook in app using Facebook ios SDK
提问by Aaradhya
I have integrated Facebook login in my app and therfore user can login with both my app account and also Facebook and do corresponding actions.For Facebook integration I have added Facebook SDK.Now when Logout button is clicked in my app it has to clear all the credentials of Facebook Account.I have gone for :
我已经在我的应用程序中集成了 Facebook 登录,因此用户可以使用我的应用程序帐户和 Facebook 登录并执行相应的操作。对于 Facebook 集成,我添加了 Facebook SDK。现在在我的应用程序中单击注销按钮时,它必须清除所有Facebook 帐户的凭据。我去了:
-(IBAction)btnlogOutClicked:(id)sender
{
[appDelegate fbDidlogout];
}
-(void)fbDidlogout
{
FBSession* session = [FBSession activeSession];
[session closeAndClearTokenInformation];
[session close];
[FBSession setActiveSession:nil];
}
But when I again click on button I m redirected directly to my account without going to Facebook Login page.
但是当我再次单击按钮时,我直接重定向到我的帐户,而无需转到 Facebook 登录页面。
How can I logout of Facebook?
如何退出 Facebook?
回答by iHulk
Using new Facebook SDK login kit just write below line and thats it..
使用新的 Facebook SDK 登录工具包,只需在下面写下一行就可以了。
[[FBSDKLoginManager new] logOut];
If using swift, make sure to have the necessary imports
如果使用 swift,请确保有必要的导入
import FBSDKLoginKit
func logout() {
FBSDKLoginManager().logOut()
}
回答by swiftBoy
For logout you should try this
对于注销,你应该试试这个
you can add the Logout button on Navigation Controller(Top Right Corner) in viewDidLoadmethod
您可以在viewDidLoad方法中的导航控制器(右上角)上添加注销按钮
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:@"Logout"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(logoutButtonWasPressed:)];
and the action method for above added buttonis
上面添加的按钮的动作方法是
-(void)logoutButtonWasPressed:(id)sender {
[FBSession.activeSession closeAndClearTokenInformation];
}
Hope this will help you!
希望能帮到你!
Edit:
编辑:
As you asked why its not asking for UserName and Password,So the reason is :
正如你问为什么它不要求用户名和密码,所以原因是:
When we integrate Facebook SDK in our app and try to login, it automatically check two places (to make sure we have already logged-in Facebook or not)
当我们在我们的应用程序中集成 Facebook SDK 并尝试登录时,它会自动检查两个地方(以确保我们已经登录 Facebook)
First of all It checks whether we have already logged-in into Facebook Native app which is installed on this device.
then It checks whether we have saved our FaceBook UserName and Password in Device Settings.
首先它会检查我们是否已经登录到安装在此设备上的 Facebook Native 应用程序。
然后它检查我们是否在设备设置中保存了我们的 Facebook 用户名和密码。
If both places we haven't logged-in then It will ask UserName and Password in application
如果两个地方我们都没有登录,那么它会在应用程序中询问用户名和密码
you can check Facebook account setup in Device Settings as shown in below screen shot,
您可以在设备设置中检查 Facebook 帐户设置,如下面的屏幕截图所示,
Press Home Button --> Settings -->Facebook
按主页按钮 --> 设置 --> Facebook
回答by Aqib Mumtaz
FBSDK is doing logout like this:
FBSDK 是这样注销的:
[FBSession.activeSession closeAndClearTokenInformation];
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logOut];
回答by Robert Waitforit Shrestha
If using swift 3 or 4:
如果使用 swift 3 或 4:
var loginManager = LoginManager()
Paste this code when some action is need to be done for logging out
当需要执行某些操作以注销时粘贴此代码
loginManager.logOut()
回答by Rushi
In your postButtonClicked
write following if else :
在你postButtonClicked
写的 if else 中:
-(void)postButtonClicked
{
_session = [[FBSession sessionForApplication:kApiKey secret:kApiSecret delegate:self] retain];
[_session resume];
posting = YES;
showSlideShow = 1;
if (_facebookName != nil)
{
[self logoutButtonClicked];
}
if (![_session isConnected])
{
self.loginDialog = nil;
_loginDialog = [[FBLoginDialog alloc] init];
[_loginDialog show];
}
else {
self.loginDialog = nil;
_loginDialog = [[FBLoginDialog alloc] init];
[_loginDialog show];
}
}