xcode 6:TwitterKit 在身份验证后不保持会话打开
xcode 6: TwitterKit not keeping session open after Authentication
我正在我的 LoginViewController 中调用一个会话,如下所示。问题是,在它验证并关闭弹出视图之后...会话 returns nil。所以它最初到达 api 并启动一个会话(第一个日志记录 nil 因为会话是打开的,第二个日志记录 "Signed in as..." 和用户名)然后当它关闭登录视图时它记录又是零。不完全使用我可能做错的东西......
难道是因为我正在使用 TWTRSession* 会话来检查会话?那会不会让我注销?还是跟我关闭popupview有关?
我对我的 Facebook 登录做了基本相同的事情,但 Facebook 会话一直保持打开状态,直到我注销...
- (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]);
}
}];
我决定也包括我的操作表,这样您就可以看到我是如何处理 login/logout 情况的:
- (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];
}
}
}];
}
所以我最终创建了自己的自定义 IBAction 来处理 Twitter 登录。这样效果更好。基本上你只需要小心检查会话是否处于活动状态:
-(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];
}
如您所见,TWTR *session 必须包含在 Twitter 共享实例中,如下所示:
[[Twitter sharedInstance] logInWithCompletion:^
(TWTRSession *session, NSError *error) {
我以前没有这样做,这就是它不起作用的原因。希望这对某人有帮助。 (现在,如果有人能帮助我使 Twitter 电子邮件正常工作……将不胜感激……)
我正在我的 LoginViewController 中调用一个会话,如下所示。问题是,在它验证并关闭弹出视图之后...会话 returns nil。所以它最初到达 api 并启动一个会话(第一个日志记录 nil 因为会话是打开的,第二个日志记录 "Signed in as..." 和用户名)然后当它关闭登录视图时它记录又是零。不完全使用我可能做错的东西......
难道是因为我正在使用 TWTRSession* 会话来检查会话?那会不会让我注销?还是跟我关闭popupview有关?
我对我的 Facebook 登录做了基本相同的事情,但 Facebook 会话一直保持打开状态,直到我注销...
- (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]);
}
}];
我决定也包括我的操作表,这样您就可以看到我是如何处理 login/logout 情况的:
- (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];
}
}
}];
}
所以我最终创建了自己的自定义 IBAction 来处理 Twitter 登录。这样效果更好。基本上你只需要小心检查会话是否处于活动状态:
-(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];
}
如您所见,TWTR *session 必须包含在 Twitter 共享实例中,如下所示:
[[Twitter sharedInstance] logInWithCompletion:^
(TWTRSession *session, NSError *error) {
我以前没有这样做,这就是它不起作用的原因。希望这对某人有帮助。 (现在,如果有人能帮助我使 Twitter 电子邮件正常工作……将不胜感激……)