在iOS 6.0中不推荐使用TWRequest - 我可以使用什么?

时间:2012-11-11 11:12:54

标签: ios twrequest social-framework

我正在为iOS应用程序开发Twitter Feed视图。我找到了TWRequest,它的工作方式与我想要的完全相同。但是:我收到一条通知:“TWRequest已被弃用:首先在iOS 6.0中弃用”。我该怎么用?

4 个答案:

答案 0 :(得分:59)

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

您几乎以与弃用TWRequest相同的方式使用它,但您需要指定它是Twitter请求而不是Facebook请求。

从iOS 6开始,整个Twitter.framework已被弃用,因为Apple将iOS和微博(中国社交网络)添加到iOS 6,他们将所有社交类别归入新的Social.framework

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

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

请务必查看documentation

答案 1 :(得分: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 !");
                         }
                     }];
            }
        }
    }];

答案 2 :(得分:0)

如果您计划通过Twitter整合TwitterKit以通过您的自定义Twitter应用程序执行推文,那么这可能会对您有所帮助。

https://stackoverflow.com/a/28602749/1740354

答案 3 :(得分: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);
    }