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

TWRequest在iOS 6.0中已被弃用-我可以使用什么替代?,ios,twrequest,social-framework,Ios,Twrequest,Social Framework,我正在为一个iOS应用程序开发一个Twitter提要视图。我找到了TWRequest,它的工作原理与我正在寻找的完全相同。但是:我收到一个通知:“TWRequest已弃用:在iOS 6.0中首次弃用”。我应该用什么来代替呢?在iOS 6上,您应该使用社交.framework。这有一个名为SLRequest的类 您使用它的方式与不推荐的TWRequest几乎相同,但您需要指定它是twitter请求,而不是facebook请求 整个Twitter.framework从iOS 6开始就被弃用了,因为苹

我正在为一个iOS应用程序开发一个Twitter提要视图。我找到了TWRequest,它的工作原理与我正在寻找的完全相同。但是:我收到一个通知:“TWRequest已弃用:在iOS 6.0中首次弃用”。我应该用什么来代替呢?

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

您使用它的方式与不推荐的
TWRequest
几乎相同,但您需要指定它是twitter请求,而不是facebook请求

整个
Twitter.framework
从iOS 6开始就被弃用了,因为苹果在iOS 6中添加了Facebook和微博(一个中国社交网络),他们将所有社交类分组到新的
social.framework

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

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

请务必查看。

以下是使用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 !");
                         }
                     }];
            }
        }
    }];

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


另一种选择是使用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);
    }
2015年:使用“”,还请注意,在responseData中,错误很可能以JSON的形式返回。