有没有办法通过SLComposeViewController回复特定的推文

时间:2013-05-29 13:26:06

标签: iphone twitter

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

2 个答案:

答案 0 :(得分:1)

好的,我们goooo。首先,我们需要在每次点击回复按钮时生成SLComposeViewController。一旦启动了SLComposeViewController,我们深入研究它并搜索“发送”按钮并禁用名为sendButtonTapped:的相关操作。在剥离本机操作后,我们将自己的自定义操作与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会照顾其余部分!!

-(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的代码是:

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

答案 1 :(得分:0)

由于SLComposeViewController继承自View Contoller,我们需要将SLComposeViewcontroller剥离到它的UITextView属性并获取其内容。然后我们将收集的信息与"in_reply_to_status_id"一起传递给Twitter API。轻松活泼的!! Lemme知道你是否需要帮助代码! Spoiler我还没有实现它..但由于我能够从SLComposerViewController检索用户输入,我认为其余部分将是小菜一碟