有没有办法通过SLComposerViewController回复特定的推文 [英] Is there a way of replying to a particular tweet via SLComposerViewController

查看:90
本文介绍了有没有办法通过SLComposerViewController回复特定的推文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用SLComposerViewController回复推文?有没有人以前做过?

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

推荐答案

好的,我们goooo。首先,我们需要在每次点击回复按钮时生成SLComposeViewController。一旦启动了SLComposeViewController,我们深入研究它并搜索发送按钮并禁用名为 sendButtonTapped:的相关操作。在剥离本机操作后,我们将自己的自定义操作与sendButton相关联。

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.

以下代码执行此操作:

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:^{}];
                }

现在我们的自定义操作(在我的情况下为replyToTweet):我们提取用户输入评论。然后将所有这些传递给Twitter API。 Twitter会照顾其余部分!!

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];
}

剥离UiTextView的代码是:

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:记住要删除SLComposeViewController的UIButton !!

IMP: Remember to strip down the UIButton of SLComposeViewController as well!!

这篇关于有没有办法通过SLComposerViewController回复特定的推文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆