2

Does anyone know how to do a simple GET request of a user's timeline using the Twitter API in iOS?
https://dev.twitter.com/rest/reference/get/statuses/user_timeline

I realize there are multiple frameworks out there but this is all I need and would prefer not to use them just for this. I also don't want to use the Social framework on iOS because it requires the user to have a Twitter account.

I've implemented this in PHP from this post here but not sure how to do the equivalent thing in iOS. https://stackoverflow.com/a/12939923/284714

Community
  • 1
  • 1
Berry Blue
  • 15,330
  • 18
  • 62
  • 113
  • You didn't mention anything about Fabric. So I would like to know if you have tried to use Fabric for iOS from Twitter or not https://docs.fabric.io/apple/fabric/overview.html ? Fabric can help you install Twitter SDK to your project and then you can use this example to From Twitter https://docs.fabric.io/apple/twitter/access-rest-api.html#construct-a-twitter-request-manually to do GET Requests – Suhaib Sep 16 '16 at 01:47
  • Yes, I'm aware of Fabric but I don't want to use it. – Berry Blue Sep 16 '16 at 01:49
  • Ok, No problem. By the way, if you create a twitter app here https://apps.twitter.com and get the access/secret key. You can use them to remove the restriction of requiring the user to have a twitter account as you mentioned above – Suhaib Sep 16 '16 at 01:56

2 Answers2

2

Here's how you do it with NSURLSession, which is the current, modern, "future proof" (lol) way of doing it on iOS/macOS:

NSURLSession *urlSession;
NSURLSessionDataTask *dataTask;
NSURL *twitterURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/user_timeline.json"];

urlSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
dataTask = [urlSession
            dataTaskWithURL:twitterURL
            completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

                // add error checking here
                NSLog(@"Got %lu bytes of data from twitter", (unsigned long)[data length]);

            }];
[dataTask resume];

And if swift is your cup of tea:

let defaultSession = URLSession(configuration: URLSessionConfiguration.default)
var dataTask: URLSessionDataTask?
let url: URL! = URL(string: "https://api.twitter.com/1.1/statuses/user_timeline.json");
dataTask = defaultSession.dataTask(with: url) {
    (data, response, error) in
    // check errors and stuff.
    print(data)
}
dataTask?.resume()
TyR
  • 718
  • 4
  • 9
0

One quick way to do this on iOS would be to use the ACAccountStore class to get one of the user's authenticated Twitter accounts, then perform a request using SLRequest. SLRequest has an account property you'd use to include the account.

Tim Johnsen
  • 1,471
  • 14
  • 33
  • Thanks for the answer. Yes, I'm aware of that class but I don't want to require the user to have a Twitter account. – Berry Blue Sep 17 '16 at 16:51
  • All Twitter API requests require account authentication as of API v1.1 – Tim Johnsen Sep 17 '16 at 17:27
  • Yes, I know but using ACAccountStore requires that the user themselves are signed in with a Twitter account. I want to get tweets from an arbitrary timeline without requiring the user to have a Twitter account. – Berry Blue Sep 17 '16 at 18:05