在Facebook登录后更改查看控制器 [英] Change View Controllers After Facebook Login

查看:148
本文介绍了在Facebook登录后更改查看控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有我的视图控制器列在下面处理Facebook登录。现在,应用程序运行并加载具有Facebook登录按钮的视图。登录后,按钮将文本更改为注销。我想要发生的是一旦用户登录,然后转到另一个视图控制器。

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.

我在故事板中添加了一个新的视图控制器,并做了一个两者之间,但我觉得我错过了一些东西。我创建的新的视图控制器被命名为homeViewController,如果这有帮助。谢谢

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


推荐答案

最初检查

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!");
                                      }


                                  }];

如果没有登录,则放置了FB的凭据。在这个委托呼叫下,您的VC在登录后显示

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);
}

另一种方法是检查

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


             }
         }];


    }

这篇关于在Facebook登录后更改查看控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆