Back again with another episode, this time we'll go into the depths of integrating Twitter Login into our application. We'll first start with getting a Fabric account at https://get.fabric.io. Then we can start installing the Twitter pods by configuring our pod file appropriately. Finally we'll integrate the TWTLoginButton along with getting the application to authenticate our user properly.
Enjoy!
!syntax-highlight
import TwitterKit
class ViewController: UIViewController, FBSDKLoginButtonDelegate, GIDSignInUIDelegate {
override func viewDidLoad() {
//...
setupTwitterButton()
}
fileprivate func setupTwitterButton() {
let twitterButton = TWTRLogInButton { (session, error) in
if let err = error {
print("Failed to login via Twitter: ", err)
return
}
//print("Successfully logged in under Twitter...")
//lets login with Firebase
guard let token = session?.authToken else { return }
guard let secret = session?.authTokenSecret else { return }
let credentials = FIRTwitterAuthProvider.credential(withToken: token, secret: secret)
FIRAuth.auth()?.signIn(with: credentials, completion: { (user, error) in
if let err = error {
print("Failed to login to Firebase with Twitter: ", err)
return
}
print("Successfully created a Firebase-Twitter user: ", user?.uid ?? "")
})
}
view.addSubview(twitterButton)
twitterButton.frame = CGRect(x: 16, y: 116 + 66 + 66 + 66, width: view.frame.width - 32, height: 50)
}
//...
}