Facebook登录后重定向到新的ViewController

时间:2013-12-14 00:46:47

标签: ios iphone objective-c xcode facebook

我已将Facebook iOS SDK添加到我的xCode Project中,因此它允许用户登录。但是,一旦用户登录,在同一个视图控制器中,他们的信息只需从他们的个人资料中更新,登录按钮就会变为登出。

但是,如果登录成功,我希望将用户重定向到DashboardViewController。我该怎么做?

这是我的.m文件

#import "ViewController.h"

#import "FriendsViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.delegate = [[UIApplication sharedApplication] delegate];
    self.facebook = [self.delegate facebook];
    self.facebook.delegate = self;

}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.loginButton setTitle:[self.facebook loginLabel] forState:UIControlStateNormal];
    self.userProfileImage.profileID = [self.facebook userProfilePicture].profileID;

    if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {

        // Yes, so just open the session.
        [self.facebook login];
    }
}

- (IBAction)performLogin:(id)sender
{
    if([self.facebook status] == logOff)
    {
        [self.facebook login];
    }
    else
    {
        [self.facebook logout];
    }
}

- (void)loginFacebookStatusUpdate
{
    [self.loginButton setTitle:[self.facebook loginLabel] forState:UIControlStateNormal];

    self.userProfileImage.profileID = [self.facebook userProfilePicture].profileID;
    self.userProfileLabel.text = [self.facebook userProfileName];
    self.userProfileBirth.text = [self.facebook userProfileBirthday];
}

- (void)getFriends:(id)sender
{


    if([self.facebook isLogged])
    {
        NSError *error;
        NSString *query = QUERY_USER_FRIENDS;

        [self.facebook submitQueryFQL:query error:error];
    }
}

- (void)fetchFQLQueryResult:(NSArray *)result error:(NSError *)error
{
    self.friends = [NSArray arrayWithArray:result];

    FriendsViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"FriendsViewController"];
    controller.friends = self.friends;

    [self presentViewController:controller animated:YES completion:nil];

}



@end

2 个答案:

答案 0 :(得分:1)

我认为您正在使用FBLoginView进行Facebook登录。因此,一旦完成登录过程,您可以通过以下方法重定向用户。你需要提供loginView。

- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView
{
         dashBoardViewController *dashBoard = [[dashBoardViewController alloc]initWithNibName:@"dashBoardViewController" bundle:nil];
 [self.navigationController pushViewController:dashBoard animated:YES];
}

同样,您也可以使用

方法处理注销
 - (void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView
{
      // Your Code...
}

希望,这会奏效。 :)

答案 1 :(得分:0)

是可以使用facebook默认功能。试试这可能是完整的帮助

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView user:(id<FBGraphUser>)user {
      NSLog(@"email address %@",[user objectForKey:@"email"]);
     NSLog(@"Token Return %@",[FBSession activeSession].accessTokenData);
     dashBoardViewController *dashBoard = [[dashBoardViewController alloc]initWithNibName:@"dashBoardViewController" bundle:nil];
     [self.navigationController pushViewController:dashBoard animated:NO];
}

它运作良好&amp;我测试过,实际上我在上一个项目中也有同样的工作。试试吧