0

我有一个小应用程序,它尝试使用 Facebook 教程中用于登录的确切方法使用 FB 进行基本登录:https ://developers.facebook.com/docs/howtos/login-with-facebook-using-ios -SDK/

期望的行为是用户能够登录到 FB 并保持登录状态,直到他们通过单击注销按钮明确选择注销。即使应用程序被强行杀死(但未删除),他们也应该在应用程序启动期间保持登录状态。

发生的情况是,单击登录按钮后,应用程序正确登录用户,但是一旦我导航回根视图控制器,然后返回 FacebookViewController,按钮再次显示登录,而不是注销。某处的某些东西正在杀死会话。

我不确定会要求什么代码,但这里有一堆:

#import "FacebookViewController.h"

@interface FacebookViewController ()

@end

@implementation FacebookViewController
@synthesize authButton;
@synthesize postSwitch;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(sessionStateChanged:)
     name:FBSessionStateChangedNotification
     object:nil];

    // Check the session for a cached token to show the proper authenticated
    // UI. However, since this is not user intitiated, do not show the login UX.
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    [appDelegate openSessionWithAllowLoginUI:NO];

    // Set toggle
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"postToFB"] isEqualToString:@"Yes"]) {
        //set to yes
        [postSwitch setOn:YES animated:YES];
    } else {
        // set to no
        [postSwitch setOn:NO animated:YES];
    }

    NSLog(@"postToFB is now %@",[[NSUserDefaults standardUserDefaults] objectForKey:@"postToFB"]);
}

- (IBAction)authButtonAction:(id)sender
{
    AppDelegate *appDelegate =
    [[UIApplication sharedApplication] delegate];

    // If the user is authenticated, log out when the button is clicked.
    // If the user is not authenticated, log in when the button is clicked.
    if (FBSession.activeSession.isOpen) {
        [appDelegate closeSession];
    } else {
        // The user has initiated a login, so call the openSession method
        // and show the login UX if necessary.
        [appDelegate openSessionWithAllowLoginUI:YES];
    }
}

- (IBAction)togglePostSwitch:(id)sender
{
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"postToFB"] isEqualToString:@"Yes"]) {
        //set to No
        [[NSUserDefaults standardUserDefaults] setObject:@"No" forKey:@"postToFB"];
    } else {
        // set to yes
        [[NSUserDefaults standardUserDefaults] setObject:@"Yes" forKey:@"postToFB"];
    }

    NSLog(@"postToFB is now %@",[[NSUserDefaults standardUserDefaults] objectForKey:@"postToFB"]);
}

- (void)sessionStateChanged:(NSNotification*)notification
{
    if (FBSession.activeSession.isOpen) {
        [self.authButton setTitle:@"Logout of Facebook" forState:UIControlStateNormal];

    } else {
        [self.authButton setTitle:@"Login to Facebook" forState:UIControlStateNormal];

    }
}

@end

MainViewController(根 VC)在 ViewDidLoad 和 ViewWillAppear 中有以下内容:

- (void)viewDidLoad
{
    [[self navigationController] setNavigationBarHidden:YES animated:NO];
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    BOOL open = [appDelegate openSessionWithAllowLoginUI:NO];
    if (open) {
        NSLog(@"User session found (MainVC viewDidLoad)");
    } else {
        NSLog(@"no session detected");
    }


}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:NO];
    [[self navigationController] setNavigationBarHidden:YES animated:NO];
}

我不清楚为什么会发生这种行为。任何帮助都会被极大地接受:)

4

1 回答 1

0

我实际上将此追溯到标记登录按钮的错误。即使按钮显示“登录”,会话也始终处于打开状态。我设法纠正了这个问题,但现在我发现该应用程序确实不会在启动时保持登录状态,但至少登录/注销按钮总是正确的!

于 2012-09-19T21:05:36.780 回答