This might help answer your question: I've figured it out just now:
I think I've used Matt Gemmell and his source code although I may have used Ben Gottlieb, I can't quite remember. Pretty sure it's Matt though.
However what I've also done is create a hidden subview that holds the actual tweeting text field, the tweet button and a return button so the user can then hide the view again.
So to summarise, I have a button which shows the originally hidden subview and while it does this, it authenticates the user. This subview then has the tweet button used to post tweets. Here is the code that I find has worked for me. Now I've only done minimal testing, but I hope it is enough for you guys to go off on your own and take it from here.
-(IBAction)shareTwit:(id)sender { //This shows the tweet view and authorises the user and engine
tweetView.hidden = NO;
if (!_engine) {
_engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate:self];
_engine.consumerKey = kOAuthConsumerKey;
_engine.consumerSecret = kOAuthConsumerSecret;
}
if (![_engine isAuthorized]) {
UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine:_engine delegate:self];
if (controller) {
[self presentModalViewController: controller animated: YES];
}
else {
[_engine sendUpdate: [NSString stringWithFormat: @"Already Updated. %@", [NSDate date]]];
}
}
}
-(IBAction)updateTwitter:(id)sender { //This button sends the tweet to Twitter.
if ([_engine isAuthorized]) {
[_engine sendUpdate:tweetTextField.text];
[tweetTextField resignFirstResponder];
confirmation.text = @"Tweet sent successfully."; //This is an optional UILabel, if you would like the user to know if the tweet has been sent
// Currently, I haven't created a method where the user is told the sending failed.
}
else {
UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine:_engine delegate:self];
if (controller) {
[self presentModalViewController: controller animated: YES];
}
else {
[_engine sendUpdate: [NSString stringWithFormat: @"Already Updated. %@", [NSDate date]]];
}
}
}
Notice I haven't used a viewDidLoad
, mainly because I didn't agree with it. I wanted the user to tap the shareTwit
button first to activate the authentication. Then within the view launched they can write their tweet. I hope this helps you!