Is is possible to reply to a tweet using SLComposerViewController? Has anyone done it before?
okay here we goooo. First we need to generate a SLComposeViewController everytime a reply button is tapped. Once dthe SLComposeViewController is initiated we dig deep into it and search for "send" button and disable associated action called sendButtonTapped:
. After stripping the the native action we associate our own custom action to the sendButton.
Heres the Code to do that:
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
userTypedTweet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[userTypedTweet setInitialText:[NSString stringWithFormat:@"%@",authorName]];
sendButton = [self tweetSendButton:userTypedTweet.view];
NSLog(@"%@",sendButton);
NSArray * actions = [sendButton actionsForTarget:userTypedTweet forControlEvent:UIControlEventTouchUpInside];
for (NSString * action in actions)
if([action isEqualToString:@"sendButtonTapped:"])
[sendButton removeTarget:userTypedTweet action:NSSelectorFromString(action) forControlEvents:UIControlEventTouchUpInside];
[sendButton addTarget:self action:@selector(replyToTheTweet) forControlEvents:UIControlEventTouchUpInside];
[self presentViewController:userTypedTweet animated:YES completion:^{}];
}
Now in our Custom Action (replyToTweet in my case): we extract the user input comments. and then pass the whole of those to the Twitter API. Twitter will take care for the rest of it!!
-(void)replyToTheTweet
{
SingletonClass *myAccount= [SingletonClass sharedobject];
UITextView * textView = [self tweetTextView:self.userTypedTweet.view];
NSLog(@"we have the value :%@",textView.text);
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setObject:[myAccount.currentTweet objectForKey:@"id_str"] forKey:@"in_reply_to_status_id"];
[parameters setObject:textView.text forKey:@"status"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update.json"] parameters:parameters];
NSLog(@"%@",request.parameters);
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (responseData)
{
NSError *parseError = nil;
id json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&parseError];
if (!json)
NSLog(@"Parse Error: %@", parseError);
else
{
UIAlertView *alertOK = [[UIAlertView alloc] initWithTitle:@"Successful" message:@"Tweet was succesfully replied to" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
dispatch_async(dispatch_get_main_queue(), ^{[alertOK show];});
}
}
else
{
NSLog(@"Request Error: %@", [error localizedDescription]);
}
}];
[self.userTypedTweet dismissViewControllerAnimated:YES completion:nil];
}
Code to Strip the UiTextView is :
- (UITextView *)tweetTextView:(UIView *)view
{
for (UIView * subview in view.subviews)
{
if ([subview isMemberOfClass:[UITextView class]])
return (UITextView *)subview;
UITextView * textView = [self tweetTextView:subview];
if (textView) return textView;
}
return nil;
}
IMP: Remember to strip down the UIButton of SLComposeViewController as well!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With