TWRequest is deprecated in iOS 6.0 - what can I use instead?
Asked Answered
C

4

25

I'm developing a Twitter Feed View for an iOS App. I found TWRequest and it works exactly like that which i was looking for. But: i get an Notice: "TWRequest is deprecated: first deprecated in iOS 6.0". What should I use instead?

Clearance answered 11/11, 2012 at 11:12 Comment(0)
R
59

On iOS 6 you should use the Social.framework. This has a class named SLRequest.

You use it almost in the same way as the deprecated TWRequest, but you need to specify that it's a twitter request as opposed to a facebook request.

The entire Twitter.framework became deprecated as of iOS 6, since Apple added Facebook and Weibo (a Chinese social network) to iOS 6, they grouped all social classes into the new Social.framework.

Note you must specify the service type for Twitter/Facebook, Example:

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

Be sure to check out the documentation.

Removal answered 11/11, 2012 at 11:14 Comment(0)
P
2

Here is a complete code to upload text + image to your Twitter Account using Twitter api

    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 !");
                         }
                     }];
            }
        }
    }];
Poe answered 12/1, 2015 at 12:35 Comment(1)
2015: Use "api.twitter.com/1.1/statuses/update.json" and also note that errors quite possibly may come back as JSON in the responseData.Preindicate
L
0

In case you plan on integrating TwitterKit by Twitter to perform the tweets via your custom twitter app then this might help you.

https://mcmap.net/q/538192/-ios-5-twitter-framework-tweeting-without-user-input-and-confirmation-modal-view-controller

Lactoprotein answered 19/2, 2015 at 9:36 Comment(0)
A
0

Another alternative is to use Twitter API. You should have Twitter framework for that.

Then do following code:

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);
    }
Aspersion answered 8/6, 2015 at 5:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.