简体   繁体   中英

xcode 6: TwitterKit not keeping session open after Authentication

I'm calling up a session in my LoginViewController as seen below. The problem is that, after it authenticates and closes the popupview... the session returns nil. So it initially reaches out to the api and starts a session (the first log records nil as the session is open and the second log records "Signed in as..." with the username) then when it closes the login view it records nil again. Not exactly use what I could be doing wrong...

Could it be because I'm using TWTRSession* session to check the session? Could that be logging me off? Or is it related to me closing the popupview?

I do essentially the same thing with my facebook login but the facebook session stays open until I logout...

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.email.delegate = self;
    self.pass.delegate = self;
    //self.view.frame = CGRectMake(self.bounds.size.width, self.bounds.size.height);
    [Fabric with:@[[Twitter sharedInstance]]];

    TWTRSession* session;
    if (session) {
        NSLog(@"Early bird %@", [session userName]);
        [self closePopup];
    }
    else {
        NSLog(@"No dice");
    }
    /*
    TWTRLogInButton *logInButton = [TWTRLogInButton buttonWithLogInCompletion:^(TWTRSession *session, NSError *error) {
        // play with Twitter session
    }];
    logInButton.center = self.view.center;
    [self.view addSubview:logInButton];
*/
    TWTRLogInButton* logInButton =  [TWTRLogInButton
                                     buttonWithLogInCompletion:
                                     ^(TWTRSession* session, NSError* error) {
                                         if (session) {
                                             NSLog(@"signed in as %@", [session userName]);
                                             [self closePopup];
                                         } else {
                                             NSLog(@"error: %@", [error localizedDescription]);
                                         }
                                     }];

I decided to include my actionsheet as well so you could see how I'm handling the login/logout situation:

- (void)sheet: (id) sender {
    NSString *email = [[NSUserDefaults standardUserDefaults] objectForKey:@"email"];
    UIActionSheet *sheet;
    TWTRSession *session;
    //if ([FBSDKAccessToken currentAccessToken]) {
    if (email == nil && [FBSDKAccessToken currentAccessToken] == nil && session == nil) {
        sheet = [[UIActionSheet alloc] initWithTitle:@"Profile"
                                                           delegate:nil // Can be another value but will be overridden when showing with handler.
                                                  cancelButtonTitle:@"Cancel"
                                             destructiveButtonTitle:@"Login"
                                                  otherButtonTitles:nil];
    }
    else
    {
        sheet = [[UIActionSheet alloc] initWithTitle:@"Profile"
                                                       delegate:nil // Can be another value but will be overridden when showing with handler.
                                              cancelButtonTitle:@"Cancel"
                                         destructiveButtonTitle:@"Logout"
                                              otherButtonTitles:@"User page", nil];
    }
        [sheet showInView:self.view
              handler:^(UIActionSheet *actionSheet, NSInteger buttonIndex) {

                  if (buttonIndex == [actionSheet cancelButtonIndex]) {
                      NSLog(@"Cancel button index tapped");
                  } else if (buttonIndex == [actionSheet destructiveButtonIndex]) {
                      if (email == nil && [FBSDKAccessToken currentAccessToken] == nil && session == nil) {
                          LoginViewController *loginView = [[LoginViewController alloc] initWithNibName:@"LoginView" bundle:[NSBundle mainBundle]];
                          //[self.view modalViewController:loginView animated:YES];
                          //[self presentModalViewController:loginView animated:YES];
                          //[self.navigationController pushViewController:loginView animated:YES];
                          loginView.delegate = (id) self;
                          [self presentPopupViewController:loginView animationType:MJPopupViewAnimationSlideTopBottom];
                          NSLog(@"Login View");
                      }
                      else
                      {
                          [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"email"];
                          FBSDKLoginManager *logout = [[FBSDKLoginManager alloc] init];
                          [logout logOut];
                      }
                  } else  {

                      NSLog(@"Button %i tapped", buttonIndex);
                      if (buttonIndex == 1) {
                          UserViewController * userView = [[UserViewController alloc] initWithNibName:@"UserView" bundle:[NSBundle mainBundle]];
                          [userView setUEmail:email];
                          [self presentViewController:userView animated:YES completion:nil];
                      }
                  }                      
              }];

}

So I ended up creating my own custom IBAction to handle twitter logins. This works much better. Basically you just have to be careful with how you check if a session is active:

-(IBAction)twlogin:(id)sender
{
    [Fabric with:@[[Twitter sharedInstance]]];
    [[Twitter sharedInstance] logInWithCompletion:^
     (TWTRSession *session, NSError *error) {

         if (session) {
             NSLog(@"signed in as %@", [session userName]);
             TWTRShareEmailViewController* shareEmailViewController =
             [[TWTRShareEmailViewController alloc]
              initWithCompletion:^(NSString* email2, NSError* error) {
                  NSLog(@"Email %@, Error: %@", email2, error);
              }];
             [self presentViewController:shareEmailViewController
                                animated:YES
                              completion:nil];
             [self closePopup];
             NSLog(@"session check %@", [session userName]);
         } else {
             NSLog(@"error: %@", [error localizedDescription]);
         }
     }];
    [[Twitter sharedInstance] logOut];
}

As you can see TWTR *session has to be included with Twitter shared instance like so:

[[Twitter sharedInstance] logInWithCompletion:^
         (TWTRSession *session, NSError *error) {

I didn't do that before, and that's why it wasn't working. Hopefully this helps someone. (now if someone would help me getting Twitter emails to work... that would be greatly appreciated...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM