推特在iOS 6& 7:在没有用户确认的情况下发布推文

时间:2014-01-30 10:23:48

标签: ios iphone ipad twitter

我正在尝试将Twitter集成到我的iOS应用程序中。

该应用程序应支持iOS versions 6.x & 7.x

我想在用户点击tweet中的按钮后直接发布UI,而无需再次在SLComposeViewController提醒中确认。

我已经阅读了以下帖子,说明如何执行此操作,问题是它们都已配置为iOS 5.x

Stack Overflow Link 1

Stack Overflow Link 2

我到底该怎么做?

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

我认为iOS 5.1 to iOS 6.0没有任何差异,Twitter api也没有iOS 6.1 to 7.0

使用iOS 5教程并使用alt +鼠标左键单击以了解有关删除或已修改的功能的信息。

Maybe this will help.

答案 1 :(得分:0)

在你提到的两个链接中,他们使用的是Twitter Api v1。很久以前已经弃用:API V1 no longer function而你应该使用V1.1,因此发布推文的网址将为https://api.twitter.com/1.1/statuses/update.json

此外,在iOS 6.0中也不推荐使用TWRequest,您应该使用SLRequest

这是一个简单的代码:

- (void) sendTweetWithoutPromp:(NSString*) tweetText
{
    NSString *url = @"https://api.twitter.com/1.1/statuses/update.json";
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    [params setObject:tweetText forKey:@"status"];

    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

    [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
        if(granted)
        {
            NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
            if ([accountsArray count] > 0)
            {
                ACAccount *twitterAccount = [accountsArray objectAtIndex:0];

                SLRequest *postRequest = [SLRequest  requestForServiceType:SLServiceTypeTwitter requestMethod:TWRequestMethodPOST URL:[NSURL URLWithString:url] parameters:params ];

                [postRequest setAccount:twitterAccount];

                [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
                 {
                     NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]];
                     NSLog(@"output = %@",output);
                     dispatch_async( dispatch_get_main_queue(), ^{
                         if (error)
                         {

                         }

                     });
                 }];
            }
            else
            {
                NSLog(@"no Account in Settings");
            }
        }
    }];

}