作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以使用 SLComposeViewController 回复推文?有人做过吗?
最佳答案
好吧,我们就到这里吧。首先,我们需要在每次点击回复按钮时生成一个 SLComposeViewController。一旦 SLComposeViewController 启动,我们就会深入研究它并搜索“发送”按钮并禁用名为 sendButtonTapped:
的关联操作。剥离 native 操作后,我们将自己的自定义操作关联到 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!
关于iphone - 有没有办法通过 SLComposeViewController 回复特定推文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16815180/
我是一名优秀的程序员,十分优秀!