How do I store & access a Twitter Fabric login session (iOS/Swift)?

旧城冷巷雨未停 提交于 2019-12-04 06:34:20

If there is currently an active session you should be able to access it just like the docs say to

Twitter.sharedInstance().session() 

If the user isn't logged in that method will return nil. If you want to know if someone is already authenticated just check to see if that method returns a value or not.

Twitter kit automatically present a login screen if user has not logged in (no session exist..). But, before calling any future API, just check whether session is valid or not. In some APIs, you may required to pass this session too.

if ([[Twitter sharedInstance] session]) {

        TWTRShareEmailViewController *shareEmailViewController =
        [[TWTRShareEmailViewController alloc]
         initWithCompletion:^(NSString *email, NSError *error) {
             NSLog(@"Email %@ | Error: %@", email, error);
         }];

        [self presentViewController:shareEmailViewController
                           animated:YES
                         completion:nil];
    } else {
        // Handle user not signed in (e.g. attempt to log in or show an alert)
    }
}

You even don't need to worry about storing and managing access token and access token secret.

To access the authToken, userName or the userID for the logged in user session in Swift 4 is:

Twitter.sharedInstance().logIn(completion: { (session, error) in
     if (session != nil) {
         print(session!.authToken)
         print(session!.userName)
         print(session!.userID)
     } else {
         print(error?.localizedDescription)
     }
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!