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

查看:21
本文介绍了有没有办法通过 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.

执行此操作的代码如下:

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

现在在我们的自定义操作中(在我的例子中是 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天全站免登陆