Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
876 views
in Technique[技术] by (71.8m points)

iphone - Is there a way of replying to a particular tweet via SLComposerViewController

Is is possible to reply to a tweet using SLComposerViewController? Has anyone done it before?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...