4slices
4slices

Reputation: 241

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.

Upvotes: 4

Views: 2773

Answers (4)

Daniel
Daniel

Reputation: 23359

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...
}

Upvotes: 5

Fogmeister
Fogmeister

Reputation: 77631

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.

Upvotes: 0

Sarafaraz Babi
Sarafaraz Babi

Reputation: 480

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..

Upvotes: 0

Frank
Frank

Reputation: 15641

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];

Upvotes: 0

Related Questions