Can I customize Login button of Twitter kit in iOS?

4.9k Views Asked by At

I have downloaded a Twitter kit framework and added code to login with Twitter. But, I don't want Login button look like that. I want a custom button for Login. Can I do that ? I only want to use this framework as this also leverages with iOS system account.

4

There are 4 best solutions below

5
Ashish Kakkad On BEST ANSWER

As per the document :

Add the code in your button press :

Objective-C

[[Twitter sharedInstance] logInWithCompletion:^
            (TWTRSession *session, NSError *error) {
  if (session) {
      NSLog(@"signed in as %@", [session userName]);
  } else {
      NSLog(@"error: %@", [error localizedDescription]);
  }
}];

Swift

Twitter.sharedInstance().logInWithCompletion {
                (session, error) -> Void in
    if (session != nil) {
        println("signed in as \(session.userName)");
    } else {
        println("error: \(error.localizedDescription)");
    }
}
2
Vizllx On

Yes,you can do customize your Twitter login button.

Just add your UIButton in your storyboard's desired View controller. Select it and open the identity inspector, now in the Class textfield, enter "TWTRLogInButton" after that create a property.

You will create something like this:-

@property (nonatomic,strong) IBOutlet TWTRLogInButton *customTwitterButton;

After that you can use it or implement it in your viewDidLoad method like this:-

self.customTwitterButton = [TWTRLogInButton buttonWithLogInCompletion:^(TWTRSession *session, NSError *error) {
    // do operations as per your wish
}];
1
NSPratik On

Twitter has provided a separate method for Sign In in the documentation. Create your custom button in xib or storyboard, give it a selector and add a method for twitter login. Here is an example:

- (IBAction)btnTwitterLogin_pressed:(id)sender
{
    [[Twitter sharedInstance] logInWithCompletion:^
     (TWTRSession *session, NSError *error) {
         if (session) {
             NSLog(@"%@", [session userID]);
             NSLog(@"%@", [session userName]);
             NSLog(@"%@", [session authToken]);
             NSLog(@"%@", [session authTokenSecret]);
         } else {
             NSLog(@"Error: %@", [error localizedDescription]);
         }
     }];
}

Hope it helps others too.

0
Naresh On

In Swift 4.2 and Xcode 10.1

Default Twitter sign in button code

let twitterLoginButton = TWTRLogInButton(logInCompletion: { session, error in
    if (session != nil) {
       print("signed in as \(session!.userName)");
    } else {
       print("error: \(error!.localizedDescription)");
    }
})

twitterLoginButton.frame  = CGRect(x: 60, y: 100, width: 200, height: 40)
self.view.addSubview(twitterLoginButton)

enter image description here

Normal login button action code

@IBAction func onClickTwitterSignin(_ sender: UIButton) {

    TWTRTwitter.sharedInstance().logIn { (session, error) in

        if (session != nil) {

            let name = session?.userName ?? ""
            print(name)
            print(session?.userID  ?? "")
            print(session?.authToken  ?? "")
            print(session?.authTokenSecret  ?? "")

            self.loadFollowers(session: session?.userID ?? "")

            let client = TWTRAPIClient.withCurrentUser()
            client.requestEmail { email, error in
                if (email != nil) {
                    let recivedEmailID = email ?? ""
                    print(recivedEmailID)
                } else {
                    print("error--: \(String(describing: error?.localizedDescription))");
                }
            }

            let twitterClient = TWTRAPIClient(userID: session?.userID)
            twitterClient.loadUser(withID: session?.userID ?? "") {(user, error) in
                print(user?.profileImageURL ?? "")
                print(user?.profileImageLargeURL ?? "")
                print(user?.screenName ?? "")
            }

            let storyboard = self.storyboard?.instantiateViewController(withIdentifier: "SVC") as! SecondViewController
            self.navigationController?.pushViewController(storyboard, animated: true)
        } else {
            print("error: \(String(describing: error?.localizedDescription))");
        }
    }

}