[英]How to logout facebook in my app using Facebook SDK
我已經在我的應用程序中集成了 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]);
}
在 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];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.