如何使用 Facebook SDK 在我的应用程序中注销 Facebook
Posted
技术标签:
【中文标题】如何使用 Facebook SDK 在我的应用程序中注销 Facebook【英文标题】:How to logout facebook in my app using Facebook SDK 【发布时间】:2015-11-13 05:38:33 【问题描述】:我已经在我的应用程序中集成了 Facebook 登录,因此用户可以使用我的应用程序帐户和 Facebook 登录并执行相应的操作。对于 Facebook 集成,我添加了Facebook SDK
。现在,当在我的应用程序中单击注销按钮时,它具有清除 Facebook 帐户的所有凭据。但是当我再次单击按钮时,我直接登录到我的帐户,而无需转到 Facebook 登录页面。
如何退出 Facebook?
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self toggleHiddenState:YES];
self.fbLoginStates.text = @"";
self.loginButton.delegate = self;
self.loginButton.readPermissions = @[@"public_profile", @"email"];
-(void)toggleHiddenState:(BOOL)shouldHide
self.lblUsername.hidden = shouldHide;
self.lblEmail.hidden = shouldHide;
self.profilePicture.hidden = shouldHide;
#pragma mark - FBLoginView Delegate method implementation
-(void)loginViewShowingLoggedInUser:(FBLoginView *)loginView
self.fbLoginStates.text = @"You are logged in.";
[self toggleHiddenState:NO];
-(void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user
NSLog(@"%@", user);
self.profilePicture.profileID = user.id;
self.lblUsername.text = user.name;
self.lblEmail.text = [user objectForKey:@"email"];
-(void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView
self.fbLoginStates.text = @"You are logged out";
[self toggleHiddenState:YES];
-(void)loginView:(FBLoginView *)loginView handleError:(NSError *)error
NSLog(@"%@", [error localizedDescription]);
【问题讨论】:
【参考方案1】:在 AppDelegate .m
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
在 AppDelegate .m
return [[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
在登录屏幕.m:-
- (void)FacebookLogin:(id)sender
// [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getFacebookData) name:@"getFacebookData" object:nil];
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login
logInWithReadPermissions: @[@"public_profile", @"email", @"user_friends"]
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
if (error)
NSLog(@"Process error");
else if (result.isCancelled)
NSLog(@"Cancelled");
else
NSLog(@"Logged in");
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphpath:@"/me"
parameters:@ @"fields": @" email",
HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
// Insert your code here
NSLog(@"%@",result);
[MBProgressHUD hideHUDForView:self.view animated:YES];
failedBlock:^
[MBProgressHUD hideHUDForView:self.view animated:YES];
UIAlertController * alert= [UIAlertController alertControllerWithTitle:APP_NAME message:@"Could not connect to the server." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okButton = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
//Handel your yes please button action here
[alert dismissViewControllerAnimated:YES completion:nil];
];
[alert addAction:okButton];
[self presentViewController:alert animated:YES completion:nil];
];
];
];
在注销屏幕.m :-
-(IBAction)Logout:(id)sender
[[FBSDKLoginManager new] logOut];
【讨论】:
嗨@varun Naharia。感谢您的回复。我正在尝试 FBSDKLoginManager 方法。但我得到了错误。 什么错误,请在您的问题中添加该错误? 错误显示如下“使用未声明的标识符 FBSDKLoginManager”。实际上我也无法导入 FBSDKLoginManager.h 类 cam u 将任何完整的 facebook 集成代码发送给我。我需要登录和注销功能 你这是什么意思?以上是关于如何使用 Facebook SDK 在我的应用程序中注销 Facebook的主要内容,如果未能解决你的问题,请参考以下文章
如何在 react native 和 facebook sdk 中管理用户帐户状态?
Facebook SDK:在我的 Android 应用程序中检索用户信息