TWTeetComposeViewController execute custom code after posting a new tweet - iphone

So, using the new Twitter framework in iOS5, we can simply post Twitter updates with this simple code:
if ([TWTweetComposeViewController canSendTweet]) {
TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];
[tweetSheet setInitialText:#"Tweeting from Steamerduck :)"];
[controller presentModalViewController:tweetSheet animated:YES];
}
However, I would like to execute some custom code after a user clicks on send here:
Is it possible to execute some custom code, some of my own methods after the TWTweetComposeViewController sends a tweet?

You can specify a completion handler that gets called after the user is done composing the tweet. It would look something like this (I haven't had a chance to test this on a device yet):
if ([TWTweetComposeViewController canSendTweet]) {
TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];
[tweetSheet setInitialText:#"Tweeting from Steamerduck :)"];
// Set a completion handler to be called when the user is done
// composing their tweet
[tweetSheet setCompletionHandler:^(TWTweetComposeViewControllerResult result) {
if (result == TWTweetComposeViewControllerResultDone) {
// Tweet was sent, do something
} else if (result == TWTweetComposeViewControllerResultCancelled) {
// Do something else if the user cancelled
[controller dismissModalViewControllerAnimated:YES];
}
}];
[controller presentModalViewController:tweetSheet animated:YES];
}

Related

Social framework - how to know if message was sent or not

I'm using Social framework in my project (iOS6), so user can make posts on Facebook or Twitter.
My question is, if there is a way to find out if user have sent a post to social network or he have canceled it and haven't published anything?
I use this code to share posts on Twitter:
-(IBAction)askOnTwitter
{
if (SYSTEM_VERSION_LESS_THAN(#"6.0"))
{
[self showErrorAlert];
twitterButton.enabled = NO;
return;
}
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
slComposeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[slComposeViewController setInitialText:#"Hey"];
[self presentViewController:slComposeViewController animated:YES completion:nil];
// So have user published a post or he haven't? How to know?
}
}
Does somebody know the trick? Thanks in advance.
You can set the completion handler for your compose view controller. There you will get the result SLComposeViewControllerResultDone or SLComposeViewControllerCancelled
__block __weak SLComposeViewController *slComposeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[slComposeViewController setCompletionHandler:^
(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultDone) {
NSLog(#"Done successfully");
}
else{
NSLog(#"Cancelled");
}
[slComposeViewController dismissViewControllerAnimated:YES completion:nil];
}];

Attach a picture to twitter post

How can I attach a picture to a twitter post like the iPhone built in photo app does?
If any body has some samplecode that will be a great help.
Thanks.
The other answers are suggesting TWTweetComposeViewController, however you should if you can avoid using this class, it's now deprecated in iOS 6,
Please see here: TWTweetComposeViewController deprecated in IOS6
And from Apple themselves, WWDC 2012, session 306 presentation PDF:
Twitter Framework
• Twitter framework is deprecated
• Do not use TWTweetComposeViewController
To use Twitter now you should use the SLComposeViewController class of the Social framework, it's usage is almost identical to TWTweetComposeViewController.
You may need to support iOS 5, in which case you have no other option then to use the TWTweetComposeViewController class, but you should make the effort to check for SLComposeViewController and use that if it's available, simply because this will save you time and effort in the near future when support for iOS 5 is dropped, the TWTweetComposeViewController class really may be gone. If you rely on the Twitter framework now for simplicity as it does work on iOS 5 and 6, you're being short sighted and you will have problems sometime later, it's only a few more lines to do this and it will mean you won't need to worry about future iOS SDK releases.
You should import Twitter.framework and Social.framework, mark them both as optional imports (not required).
Example code:
UIImage *myImage = [...]; // an image
if( NSClassFromString(#"SLComposeViewController") ){
// We have the Social framework in our iOS system
// iOS 6 and later will use this
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]){
SLComposeViewController *twitterCompose = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[twitterCompose addImage:myImage]; // Adding your UIImage
twitterCompose.completionHandler = ^(SLComposeViewControllerResult result){
// Handle result, dismiss view controller
[self dismissViewControllerAnimated:YES
completion:nil];
};
[self presentViewController:twitterCompose
animated:YES
completion:nil];
}else{
// the user does not have Twitter set up
}
}else if( NSClassFromString(#"TWTweetComposeViewController") ){
// We don't have the Social framework, work with the Twitter framework
// iOS 5 only will use this
if( [TWTweetComposeViewController canSendTweet] ){
TWTweetComposeViewController *twitterCompose = [[TWTweetComposeViewController alloc] init];
[twitterCompose addImage:myImage];
twitterCompose.completionHandler = ^(TWTweetComposeViewControllerResult result){
// Handle result, dismiss view controller
[self dismissViewControllerAnimated:YES
completion:nil];
};
[self presentViewController:twitterCompose
animated:YES
completion:nil];
}else{
// the user hasn't go Twitter set up on their device.
}
}else{
// Wow you're going retro with this app,
// you must be on iOS 4 if you ever get here...
}
Here how i use it:
NSLog(#"Ready to Tweet.");
TWTweetComposeViewController *tweetComposer = [[TWTweetComposeViewController alloc] init];
[tweetComposer setInitialText:[NSString stringWithFormat:#"My message"]];
[tweetComposer addImage:[UIImage imageNamed:#"114x114"]];
[tweetComposer addURL:[NSURL URLWithString:#"http://myPage"]];
tweetComposer.completionHandler = ^(TWTweetComposeViewControllerResult result){
if(result == TWTweetComposeViewControllerResultDone){
NSLog(#"Tweeted.");
} else if(result == TWTweetComposeViewControllerResultCancelled) {
NSLog(#"Cancelled.");
}
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
};
[self presentModalViewController:tweetComposer animated:YES];
if you are using ios 5.0 then you can directly post image like
Add Framwork twitter.framework
import Twitter/TWTweetComposeViewController.h
-(void)postToTwittert
{
Class TWTweetComposeViewControllerClass = NSClassFromString(#"TWTweetComposeViewController");
if (TWTweetComposeViewControllerClass != nil) {
if([TWTweetComposeViewControllerClass respondsToSelector:#selector(canSendTweet)]) {
TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
[twitter setInitialText:#"text"];
[twitter addImage:[UIImage imageNamed:#"imagename"]];
[twitter addURL:[NSURL URLWithString:#"http://www.google.com"]];
[self presentViewController:twitter animated:YES completion:nil];
twitter.completionHandler = ^(TWTweetComposeViewControllerResult res) {
if(res == TWTweetComposeViewControllerResultDone)
{
NSLog(#"success for twitter post");
}
else if(res == TWTweetComposeViewControllerResultCancelled)
{
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Canceled" message:#"Your Tweet was not posted" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
[self dismissModalViewControllerAnimated:YES];
};
}
}
}
call this function where you wants twitter post
and do appropriate changes that you want..
Best luck..
I just use UIActivityViewController to post to Twitter now.
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:#[#"Default Text", [UIImage imageNamed:#"imageName"]] applicationActivities:nil];
[self presentViewController:controller animated:YES completion:nil];
This will present a controller where the user can decide what to do (Post to Twitter, Post to Facebook, etc...)
It then uses the system tweet sheet etc... to do it.
You don't have to provide the default text. This can be overwritten anyway.
Oh, also, no frameworks required for this.

Cancel button on MFMessageComposeViewController does not show up

In my iPhone application, I have just implemented in-app SMS functionality. SMS functionality is working fine. But after opening MFMessageComposeViewController, if user wants to cancel sending sms, they have no option. The only option left is to send an sms, then only return on previous view. There should be a cancel button on navigation bar just as it is in the email composer. Below is the line of code that I wrote to have in-app sms functionality:
-(void) smsComposer{
MFMessageComposeViewController *_smsCompose = [[MFMessageComposeViewController alloc] init];
if ([MFMessageComposeViewController canSendText]) {
_smsCompose.body = #"SMS BODY";
_smsCompose.messageComposeDelegate = self;
[self presentModalViewController:_smsCompose animated:YES];
}
}
Is there anything that I am missing?
Thanks in advance,
PC
Try This....
in .h file
#import <MessageUI/MFMessageComposeViewController.h>
and
#interface TestViewController : UIViewController <MFMessageComposeViewControllerDelegate>
And then Button Click method
-(void)buttonPressed:(UIButton *)button
{
[self sendSMS:#"Body of SMS..." recipientList:[NSArray arrayWithObjects:#"+1-111-222-3333", #"111-333-4444", nil]];
}
MFMessageComposeViewController to create the SMS content and another method for handling the user interaction with the SMS dialog.
-(void)sendSMS:(NSString *)bodyOfMessage recipientList:(NSArray *)recipients
{
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
controller.body = bodyOfMessage;
controller.recipients = recipients;
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
}
And
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self dismissModalViewControllerAnimated:YES];
if (result == MessageComposeResultCancelled)
NSLog(#"Message cancelled")
else if (result == MessageComposeResultSent)
NSLog(#"Message sent")
else
NSLog(#"Message failed")
}
And remember: You cannot send SMS messages from within the simulator. Test on Device.

Twitter in iOS 5 leaking memory with ARC

Here is my code:
case kTweetIndex:
{
TWTweetComposeViewController *tweetSheet =
[[TWTweetComposeViewController alloc] init];
[tweetSheet setInitialText:[NSString stringWithFormat:#"%#",self.item.link]];
[self presentModalViewController:tweetSheet animated:YES];
if (![TWTweetComposeViewController canSendTweet])
{
NSLog(#"Can't Send tweet");
}
}
This is just a massive amount of leak caused by twitter, and i don't know how to handle it. Thanks!
Make Property of TWTweetComposeViewController or twitter class or you can one thing more alloc it in AppDelegate Class. It will work.

SMS option not working properly

I am using xcode 4.2 and i am developing an iphone APP , part of this app is sending SMSs
here is the code that I typed:
-(IBAction)SMSbutton{
MFMessageComposeViewController *sms = [[MFMessageComposeViewController alloc] init];
if ([MFMessageComposeViewController canSendText]) {
sms.body= [NSString stringWithFormat: #"text"];
sms.messageComposeDelegate = self;
[self presentModalViewController:sms animated:YES];
}
I can open the SMS application and send SMS but the problem is whether I click on the "cancel" or the "send" button the message application does not go away . how to do that ?
thanks
Set your calling class to be a delegate of the message composer, then catch the cancel and send events to dismiss the modal view controller as needed.
Set your header file to adhere to the MFMessageComposeViewControllerDelegate, and when you initialize the composer, set the message delegate to self:
MFMessageComposeViewController *george = [MFMessageComposeViewController alloc] init];
george.messageComposeDelegate = self;
Then implement the delegate method...easiest way is:
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self dismissModalViewControllerAnimated:YES];
}
...but you can catch and deal with errors and events as necessary.
Implement messageComposeViewController:didFinishWithResult: in your delegate"
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
if(result == MessageComposeResultCancelled) {
//Message cancelled
} else if(result == MessageComposeResultSent) {
//Message sent
}
[self dismissModalViewControllerAnimated:YES];
}
Have you tried dismissViewControllerAnimated:completion: or dismissModalViewControllerAnimated:?
[self dismissModalViewControllerAnimated: YES];