25

我正在为 iOS 应用程序开发 Twitter 提要视图。我找到了 TWRequest,它的工作方式与我正在寻找的完全一样。但是:我收到一条通知:“TWRequest 已弃用:iOS 6.0 中首次弃用”。我应该改用什么?

4

4 回答 4

59

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

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

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

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

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

请务必查看文档

于 2012-11-11T11:14:57.737 回答
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 !");
                         }
                     }];
            }
        }
    }];
于 2015-01-12T12:35:01.380 回答
0

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

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

于 2015-02-19T09:36:27.720 回答
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);
    }
于 2015-06-08T05:10:20.120 回答