Search code examples
iosobjective-ciphonetwitter

Trouble with Twitter Login Button


In my iOS App (Objective-C) I authenticate a user with a TWTRLogInButton.

As per the instructions, I started off with putting this in the viewDidLoad of my viewcontroller.m

TWTRLogInButton *logInButton = [TWTRLogInButton buttonWithLogInCompletion:^(TWTRSession *session, NSError *error) {
///
}];
    logInButton.center = self.view.center;
    [self.view addSubview:logInButton];

With all the other configurations in place, that works just fine.

However, in order to have more control over the position and size of the Twitter Login Button, I would like to add that button by means of interface builder.

So, in my viewcontroller.h I added:

@property (retain, nonatomic) IBOutlet TWTRLogInButton *twitterButton;

On my .xib (yes, I know) I added a UIView, classed it as a "TWTRLogInButton" and connected it to 'twitterButton'

Then, in my viewcontroller.m, in viewDidLoad, I do

 self.twitterButton = [TWTRLogInButton buttonWithLogInCompletion:^(TWTRSession *session, NSError *error) {
    ///
    }];

But somehow, when I tap the button, after the Twitter Dialog is dismissed, the completion is not called.

What gives?


Solution

  • It's explained in the Twitter Community forum and makes sense (didn't test it), but I'll post it here to have an answer on StackOverflow.

    Your button is declared as such and is present and connected in your Storyboard/Xib:

    @property (retain, nonatomic) IBOutlet TWTRLogInButton *twitterButton;
    

    From the doc of +(instancetype)buttonWithLogInCompletion:(TWTRLogInCompletion)completion (there is +(instancetype)) and the doc says (I put in bold the important part)

    Returns a new log in button which launches Twitter log in when tapped and calls completion when logging in succeeds or fails.

    So when you do:

    self.twitterButton = [TWTRLogInButton buttonWithLogInCompletion:^(TWTRSession *session, NSError *error) { 
        //Code for completionHandler
    }];
    

    You are creating a whole new object and overriding the previous one connected to your xib/storyboard you "lose the connection" between your code and the one you previously set in your xib/storyboard.

    How to fix it, the button has a property to set, the completion handler.

    @property (nonatomic, copy) TWTRLogInCompletion logInCompletion
    

    So set it:

    [self.twitterButton setLogInCompletion = ^(TWTRSession *session, NSError *error){
        //Code for completionHandler
    }];