TWRequest在iOS 6.0中已经被弃用 - 我可以用什么替代它?

25

我正在为一款iOS应用程序开发Twitter Feed视图。我找到了TWRequest并且它完全符合我的要求。但是:我收到了一个通知:“TWRequest被弃用:在iOS 6.0中首次被弃用”。我应该使用什么替代品?

我正在为iOS应用程序开发Twitter Feed视图。我发现TWRequest符合我的需求,但是它已经被弃用,因此需要寻找替代品。
4个回答

59

在iOS 6中,您应该使用Social.framework。它有一个名为SLRequest的类。

您可以几乎以与过时的TWRequest相同的方式使用它,但是您需要指定它是Twitter请求还是Facebook请求。

自iOS 6起,整个Twitter.framework已被弃用,因为苹果将Facebook和微博(一种中国社交网络)添加到了iOS 6中,他们将所有社交类别都分组到了新的Social.framework中。

请注意,您必须为Twitter/Facebook指定服务类型,例如:

SLRequest *aRequest  = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                          requestMethod:SLRequestMethodPOST
                                                    URL:myurl
                                             parameters:myparams];

一定要查看文档


2
这是使用Twitter api上传文本和图像到您的Twitter帐户的完整代码:
    UIImage *img = [UIImage imageNamed:@"twitterImage.png"];
    ACAccountStore *account = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    [account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
        if (granted == YES) {
            // Populate array with all available Twitter accounts
            NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
            if ([arrayOfAccounts count] > 0) {
                ACAccount *acct = [arrayOfAccounts objectAtIndex:0];
                NSDictionary *message = @{@"status": @"From my app"};
                NSURL *requestURL = [NSURL URLWithString:@"https://upload.twitter.com/1/statuses/update_with_media.json"];
                SLRequest *postRequest = [SLRequest
                                                  requestForServiceType:SLServiceTypeTwitter
                                                  requestMethod:SLRequestMethodPOST
                                                  URL:requestURL parameters:message];
                NSData *data = UIImagePNGRepresentation(img);
                [postRequest addMultipartData:data withName:@"media" type:@"image/png" filename:@"TestImage"];
                postRequest.account = acct;

                [postRequest performRequestWithHandler:
                     ^(NSData *responseData, NSHTTPURLResponse
                       *urlResponse, NSError *error)
                     {
                         if (error) {
                             NSLog(@"%@",error.description);
                         }
                         else {
                             NSLog(@"Upload Sucess !");
                         }
                     }];
            }
        }
    }];

1
2015年:请使用“https://api.twitter.com/1.1/statuses/update.json”,并注意错误很可能会以JSON格式返回到responseData中。 - prewett

0

另一个选择是使用Twitter API。您应该有Twitter框架。

然后执行以下代码:

NSString *statusesShowEndpoint = @"https://api.twitter.com/1.1/statuses/update.json";
NSDictionary *params = @{@"status": @"Hello, my first autopost tweet..."};

    NSError *clientError;
    NSURLRequest *request = [[[Twitter sharedInstance] APIClient]
                             URLRequestWithMethod:@"POST"
                             URL:statusesShowEndpoint
                             parameters:params
                             error:&clientError];

    if (request) {
        [[[Twitter sharedInstance] APIClient]
         sendTwitterRequest:request
         completion:^(NSURLResponse *response,
                      NSData  *data,
                      NSError *connectionError) {
             if (data) {
                 // handle the response data e.g.
                 NSError *jsonError;
                 NSDictionary *dicResponse = [NSJSONSerialization
                                               JSONObjectWithData:data
                                               options:0
                                               error:&jsonError];
                 NSLog(@"%@",[dicResponse description]);
             }
             else {
                 NSLog(@"Error code: %ld | Error description: %@", (long)[connectionError code], [connectionError localizedDescription]);
             }
         }];
    }
    else {
        NSLog(@"Error: %@", clientError);
    }

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接