Attach a picture to Twitter

How can I attach an image to a Twitter post, how does an iPhone created in a photo app do? If any authority has an example code , this will be a big help. Thanks.

+4
source share
4 answers

Other answers suggest TWTweetComposeViewController , however you should, if you can avoid using this class, now it's deprecated in iOS 6.

See here: TWTweetComposeViewController deprecated in iOS6

And from Apple, WWDC 2012, the presentation of 306 PDF presentations :

Twitter structure

• Twitter outdated

• Do not use TWTweetComposeViewController

To use Twitter now, you must use the SLComposeViewController class of the SLComposeViewController structure, its use is almost identical to TWTweetComposeViewController .

You may need support for iOS 5, in which case you have no other option for using the TWTweetComposeViewController class, but you should make an effort to check on the SLComposeViewController and use it if available, simply because it will save you time and effort in the near future when iOS 5 support is disabled, the TWTweetComposeViewController class TWTweetComposeViewController indeed disappear. If you now use the Twitter platform for simplicity, as it works on iOS 5 and 6, you will be noticed and you will have problems sometime later, these are just a few lines for this and this will mean that you do not need will worry about future releases of the iOS SDK.

You should import Twitter.framework and Social.framework , mark them as an optional import (optional).

Code example:

 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... } 
+5
source

Here is 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]; 
0
source

if you are using ios 5.0, you can directly send the image, for example

Add twitter.framework 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 want to post twitter

and make the necessary changes.

Good luck ..

0
source

I am just using the 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 introduce a controller in which the user can decide what to do (send to Twitter, send to Facebook, etc.)

He then uses a system tweet list, etc. to do this.

You do not need to provide default text. It can be overwritten anyway.

Oh, there are also no frameworks needed for this.

0
source

Source: https://habr.com/ru/post/1439953/


All Articles