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.
Related
I'm developing an app. I'm going to publish to an app store. But client needs to share this app with friends. I have a button to share my app. How to share the app? I read some doc they mentioned, use [[UIApplication SharedApplication] openURL:#" url"]. I don't have my app URL. Because I didn't submit the app. Is it possible to share my app?
If you want to know what the URL of your application will be before it has been submitted/approved, Apple provides a shorthand URL you can use, of the form:
http://itunes.com/apps/appname
For example:
http://itunes.com/apps/AngryBirds
If you want the specific URL with the application ID, you could submit v1.0.0 without the "Share" link, then immediately submit a 1.0.1 update with the link included.
Url can be generated via app id. Its the best way to do it coz if app name changes, url could change. But app id wont change.
You can get your app id on itunesConnect.apple.com or via itunes.
//https://itunes.apple.com/app/id_hereisyourappID
Let see how to do the sharing.
I will show you 4 ways to share your link. Via Facebook, Twitter, Mail and SMS.
For Facebook and Twitter.
Add Accounts and Social framework. And in your header import.
#import <Social/Social.h>
#import <Accounts/Accounts.h>
when you want to share link use the following code
SLComposeViewController *mySLComposerSheet = [[SLComposeViewController alloc] init];
mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];//for twitter use SLServiceTypeTwitter
[mySLComposerSheet addURL:[NSURL URLWithString:#"https://itunes.apple.com/app/id123456789"]];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
Lets do it with mail add MessageUI framework and import
#import <MessageUI/MessageUI.h>
And the delegate MFMailComposeViewControllerDelegate
in your code
MFMailComposeViewController* mailComposer = [[MFMailComposeViewController alloc] init];
[mailComposer setMailComposeDelegate:self];
[mailComposer setModalPresentationStyle:UIModalPresentationFormSheet];
[mailComposer setMessageBody:#"https://itunes.apple.com/app/id123456789" isHTML:NO];
[self presentViewController:mailComposer animated:YES completion:nil];
And add delegate method
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[controller dismissViewControllerAnimated:YES completion:nil];
}
You can create html content for mail body.
Lets share it via SMS
Add delegate MFMessageComposeViewControllerDelegate
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if([MFMessageComposeViewController canSendText])
{
controller.body = #"https://itunes.apple.com/app/id123456789";
controller.messageComposeDelegate = self;
[self presentViewController:controller animated:YES completion:nil];
}
And add delegate method.
-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[controller dismissViewControllerAnimated:YES completion:nil];
}
I hope this helps.
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];
}];
I am posting to Facebook using the social api of iOS 6.
I am using the tradicional SLComposeViewController, like in..
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
...
at some point I do
[controller addImage:imageFacebook];
where imageFacebook is a screenshot that is 280 pixels high and then I post to Facebook.
My problem is this. I am following similar approach with the twitter API, but when that little window shows, there is a thumbnail of the image I am sending, hold by a clips on the right side, but on the facebook counterpart I see this safari-like icon. Is this normal? I would be a happy man if I was seen here a miniature of the image being posted, as I saw with the twitter API...
NOTE: the image is being posted correctly to Facebook... my problem is this icon on the posting window.
Is there a way to change that icon?
thanks
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled) {
NSLog(#"Cancelled");
} else {
NSLog(#"Done");
}
[controller dismissViewControllerAnimated:YES completion:Nil];
};
controller.completionHandler = myBlock;
[controller setInitialText:#"Enter Your Text Here:"];
[controller addURL:[NSURL URLWithString:#"http://www.google.com"]];
[controller addImage:[UIImage imageNamed:#"image001-794043.jpeg"]];
[self presentViewController:controller animated:YES completion:Nil];
} else {
NSLog(#"UnAvailable");
}
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.
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];
}