Change View Controllers After Facebook Login

<img src="https://i.stack.imgur.com/1fq5n.png" alt="enter image description here"> I have my view controller listed below that handles the Facebook Login. As it is now, the app runs and loads a view that has the Facebook login button. Once logged in, the button changes the text to "Log out". What I would like to happen is once a user is logged in, it then goes to another view controller.

I have added a new view controller in storyboard and did a "segue" between the two, but I feel like I am missing something. The new view controller I made is named "homeViewController" if that helps. Thanks

#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.loginView.readPermissions = @[@"public_profile", @"email", @"user_friends"]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user { NSLog(@"%@", user.name); } - (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView { NSLog(@"You are logged in! :) "); } - (void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView { NSLog(@"You are logged out! :( "); } - (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error { NSString *alertMessage, *alertTitle; if ([FBErrorUtility shouldNotifyUserForError:error]) { alertTitle = @"Facebook error"; alertMessage = [FBErrorUtility userMessageForError:error]; } else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession) { alertTitle = @"Session Error"; alertMessage = @"Your current session is no longer valid. Please log in again."; } else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) { NSLog(@"user cancelled login"); } else { alertTitle = @"Something went wrong"; alertMessage = @"Please try again later."; NSLog(@"Unexpected error:%@", error); } if (alertMessage) { [[[UIAlertView alloc] initWithTitle:alertTitle message:alertMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]; } } @end

Answer1:

The segue should be all you need as long as it is attached to the Facebook login button.

If you want you could call

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender { if(currentUser ) { //this checks that the user logged in before moving to the next view return YES; } else return NO; }

To check that the user successfully logged in before moving to the homeViewController. you would do something like the code above

Answer2:

Check this initially

FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) { if (status == FBSessionStateOpen || status == FBSessionStateOpenTokenExtended) { //Call your view controller after login NSLog(@"Logged in!"); } }];

If not logged in, after putting credentials of FB. Under this delegate call your VC to display after logged in

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user { //Call your view controller after login NSLog(@"%@", user.name); }

Another way is check

if (![FBSession.activeSession isOpen]) { // presentLoginViewController return; } else { [[FBRequest requestForMe] startWithCompletionHandler: ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) { if (!error) { //self.label.text = user.name; //self.userProfileImage.profileID = [user objectForKey:@"id"]; NSLog(@"FB id : %@",appContext.userName); //Present your view controller after logged in } }]; }

人吐槽 人点赞

Recommend

Comment

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:Change View Controllers After Facebook Login