how to display tweet after it was sent? - iphone

I'm working on a twitter app and i want to immediately display tweet after sending. I tried to use reloadData, but it didn't work. Can anyone help me? Thank you!
Here's my code:
- (IBAction)postNewTweet:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:#""];
[self presentViewController:tweetSheet animated:YES completion:nil];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tweetTableView reloadData];
});
}
else
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Sorry"
message:#"You can't send a tweet right now, make sure your device has an internet connection and you have at least one Twitter account setup"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}

Blaze, you are reloading the table view right after presenting the compose sheet, so it reload the table right there - and you are passing the completion block as nil, so nothing will happen when user finishes tweeting.
Also, you need to be sure that you are reloading not only the table, but the array or any other object that is the source of tweetTableView.
For example:
When calling the tweet compose sheet:
[self presentViewController:tweetSheet animated:YES completion:^(SLComposeViewControllerResult result)
{
dispatch_async(dispatch_get_main_queue(), ^{
[self reloadTweets];
});
}];
And then, you reload the tweets source and only then the table
-(void)reloadTweets
{
tweetsArray = .... //load the tweets from the internet, and only then reload the table
[self.tweetTableView reloadData];
}

Related

Social sharing url of current webview iPhone. ( Xcode)

so I have my app setup with share action button that shares the current URL to Twitter or Facebook however when I click the Facebook button it shows a Twiiter share sheet. The (Tiwtter option works fine)
After I click the FACEBOOK option, when testing on iPhone the standard twiiter share sheet appears.
- (IBAction)social:(id)sender {
UIActionSheet *share = [[UIActionSheet alloc] initWithTitle:#"Pass on the news!" delegate:self cancelButtonTitle:#"OK" destructiveButtonTitle:nil otherButtonTitles:#"Post to Twitter", #"Post to Facebook", nil];
//You must show the action sheet for the user to see it.
[share showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
//Each button title we gave to our action sheet is given a tag starting with 0.
if (actionSheet.tag == 0) {
//Check Twitter accessibility and at least one account is setup.
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *tweetSheet =[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet addURL:self.newsItemWebView.request.URL]; //This is setting the initial text for our share card.
[tweetSheet setInitialText:#"Check out this article I found using the 'Pass' iPhone app:, "];
//Brings up the little share card with the test we have pre defind.
[self presentViewController:tweetSheet animated:YES completion:nil];
} else {
//This alreat tells the user that they can't use built in socal interegration and why they can't.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry" message:#"You can't send a tweet right now, make sure you have at least one Twitter account setup and your device is using iOS6 or above!." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
} else if (actionSheet.tag == 1) {
//Check Facebook accessibility and at least one account is setup.
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *facebookSheet =[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[facebookSheet addURL:self.newsItemWebView.request.URL];
//This is setting the initial text for our share card.
[facebookSheet setInitialText:#"Check out this article I found using the 'Pass' iPhone app:"];
//Brings up the little share card with the test we have pre defind.
[self presentViewController:facebookSheet animated:YES completion:nil];
} else {
//This alreat tells the user that they can't use built in socal interegration and why they can't.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry" message:#"You can't post a Facebook post right now, make sure you have at least one Facebook account setup and your device is using iOS6 or above!." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}
}
You are using actionSheet.tag which will be the same for the entiere actionsheet.
If you want to know which button is pressed you should use buttonIndex in your if statements.
Even better is to use UIActivityViewController.
NSArray *items = #[self.newsItemWebView.request.URL];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
activityViewController.completionHandler = ^(NSString *activityType, BOOL completed) {
// do any thing you want when the user finishes the share activity.
// Like present a message that that activity has completed or not.
};
[self presentViewController:activityViewController animated:YES completion:nil];

Email Signature App Iphone

I am making an email signature application that allows user to make signatures and use them to send with emails, Their is signature name(Text Field), content(Text View) and image(Image View) and i am saving them in Database so that if the user selects the signature name from the table view that is on the second view the preview will show up on the same view like if i select signature 1 from table view then in preview section the signature image should show up with the signature content in (Text View), and then on the same view we press send(Button) the text and image from the Text View of preview section will be copied to clipboard and then in third view i can paste it in the message section and send the email, is it possible to do that if yes how can i implement it or any other idea how to do this ?
i have this one method for send email with image and message .. just add MFMessageComposeViewControllerDelegate in .h file and add framework MessageUI.framework in your project
-(void)sendMailWithImage:(NSString *)message Image:(UIImage *)image{
if ([MFMailComposeViewController canSendMail])
{
UIImage *tempImageSave=image;
MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
NSString *mailBody = message;
NSData *imageData = UIImagePNGRepresentation(tempImageSave);
[mailComposeViewController addAttachmentData:imageData mimeType:#"image/png" fileName:#"Testing"];
[mailComposeViewController setMessageBody:mailBody isHTML:NO];
mailComposeViewController.mailComposeDelegate = self;
[self presentViewController:mailComposeViewController animated:YES completion:nil];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"e-Mail Sending Alert"
message:#"You can't send a mail"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
and this bellow method is delegate method of MFMessageComposeViewControllerDelegate
#pragma mark - MFMessage Delegate
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
if (result == MFMailComposeResultSent)
{
NSLog(#"\n\n Email Sent");
}
[self dismissViewControllerAnimated:YES completion:nil];
}
i hope this help you...

Add Text in TweetSheet when an UIAlert is pressed

Can anyone tell me how i can add text in TweetSheet when an UIAlert Title button is pressed?
Currently i am using this code to achieve it, but failed so far.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];
if ([title isEqualToString:#"Add custom text"]) {
UIAlertView *alert= [[UIAlertView alloc] initWithTitle:#"Add a template" message:nil delegate:self cancelButtonTitle:#"Hi it was nice meeting you" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
if ([title isEqualToString:#"Hi it was nice meeting you"]) {
[tweetSheet setInitialText:#"hey it was nice meeting you"];
}
}
Can anyone tell me what's wrong here?
You're not presenting your TWTweetComposeViewController you can do this simply by adding :
[self presentModalViewController:tweetSheet animated:YES];
I guess this code is only for a simple test tweet.
For a serious project you should always:
Check if the user can send tweets by calling +(BOOL)canSendTweet before trying to show the TWTweetComposeViewController
If not sure about the properties you'll be setting (text, images, urls), you need to check if adding them was successful : -(BOOL)addImage:(UIImage *)image, -(BOOL)addURL:(NSURL *)url and -(BOOL)setInitialText:(NSString *)text all return YES if it was successful and NO if not.
You can set a completion handler to perform different actions depending on what the user decided (send tweet/cancel)
The WWDC session about Twitter integration from 2011 is very simple and detailed, you should watch it and read the documentation if you need more information.

TWTweetComposeViewController not dismissing on iPad simulator

In my app I have an action sheet and one of its buttons opens the TWTweetComposeViewController modally. On iPhone simulator the cancel button on the tweet composer works fine and dismisses the view. However, on iPad simulator the cancel button does not work and the tweet composer view remains on the screen. It is even weirder because after pressing the cancel button, the keyboard retracts and the underlying views become active. It behaves as if the view has been dismissed but its is still there.
The code I used when the user pressed the action button is:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:#"Open in Safari"]){
[[UIApplication sharedApplication] openURL:[self.webView.request URL]];
}else if ([buttonTitle isEqualToString:#"Twitter"]){
if ([TWTweetComposeViewController canSendTweet]){
TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];
[tweetSheet addURL:[self.webView.request URL]];
tweetSheet.completionHandler = ^(TWTweetComposeViewControllerResult result){
if (result == TWTweetComposeViewControllerResultCancelled){
[self dismissModalViewControllerAnimated:YES];
}
};
[self presentModalViewController:tweetSheet animated:YES];
}else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Twitter error" message:#"You can't send a tweet right now, make sure your device has an internet connection and you have at least one Twitter account setup" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}
}
Do you have any idea on how to solve this problem or is it a bug of the simulator?
P.S.: My app is a tabbar app and this code is called from one of of the view controller of the tab bar.
I'm having this same problem on the actual device. It turns out this is a bug in Apple's SDK for TWTweetComposeViewController.
See the bug report here on OpenRadar: http://openradar.appspot.com/radar?id=1484405.
When a completionHandler block is added to
TWTweetComposeViewController, the completion handler needs to call
-[UIViewController dismissModalViewControllerAnimated:], even though the view for the tweet composer dismisses itself with its cancel or
send buttons. Failure to do so causes touch events to not reach the
view that spawned the tweet composer.
Just thought I'd add how I'm doing things, even though this is not correctly following memory guidelines, it's a workaround:
[compose setCompletionHandler:^(TWTweetComposeViewControllerResult result){
dispatch_async(dispatch_get_main_queue(), ^{
if(self.delegate != nil)
{
if (result == TWTweetComposeViewControllerResultDone)
{
[self.delegate twitterOperation:TETwitterOperationTweet
completedSuccessfully:YES
withResponseString:#"Tweet Successful"];
}
else if(result == TWTweetComposeViewControllerResultCancelled)
{
[self.delegate twitterOperation:TETwitterOperationTweet
completedSuccessfully:NO
withResponseString:#"Tweet Cancelled"];
}
}
// Dismiss per Apple's Twitter example
[self.shownInViewController dismissViewControllerAnimated:YES
completion:nil];
// Yuck. But it's necessary.
[compose release];
});

TWTweetComposeViewController freezes my app completely on alloc-init

I've got an iPhone app that uses the Twitter framework in iOS 5. I've noticed that when I try to tweet something, the app completely freezes on the "alloc init" line of code.
Here's my code. Anybody see something wrong with it?
-(void)tweet {
NSLog(#"Tweeting");
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
if (stringToShare.length < 140) {
TWTweetComposeViewController *tweetController = [[TWTweetComposeViewController alloc] init];
[tweetController setInitialText:stringToShare];
[self presentModalViewController:tweetController animated:YES];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Easy there, Hemingway." message:[NSString stringWithFormat:#"Your note has %d characters, which exceeds Twitter's limit of 140.", stringToShare.length]delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
UPDATE: This method is called from a modal view controller. Could that be the issue?
UPDATE 2: Here's the line of code that's used to detect if the user can tweet:
if ([TWTweetComposeViewController canSendTweet]) {
[availableActions addObject:#"Tweet"];
}
I know that the issue is not constant --- it seems to occur on my phone, but not my co-founder's.