Overview
TWRequest is a class in the Twitter framework for iOS5 that encapsulates HTTP requests to Twitter’s REST API. The object can also handle authenticating users for the caller.
Recommended Reading
The following articles on Apple's Developer Site will help you as you work with TWRequest:
How do I perform requests with TWRequest?
With TWRequest, you can perform both unauthenticated requests and authenticated requests to the Twitter REST API. The real power lies in the seamless way it handles authenticated requests, but to start, we will demonstrate how you can use it to generate unauthenticated requests to the API.
Unauthenticated Requests
For this example, consider a situation where you would like to retrieve the public timeline for a Twitter user with GET statuses/user_timeline. The one required parameter that we must pass to the endpoint is screen_name, but for illustration purposes, we will also pass other options to the endpoint: count, to limit the returned results, include_entities to see the entities for each object, and include_rts to include the user’s retweets in the responses.
- screen_name - theSeanCook
- count - 5
- include_entities - 1
- include_rts - 1
The equivalent HTTP request would be of this format:
GET http://api.twitter.com/1/statuses/user_timeline.json?screen_name=theSeanCook&count;=5&include;_entities=1&include;_rts=1
To use TWRequest to make this unauthenticated request, we would do the following:
Source Code Notes:
- This example utilizes Automatic Reference Counting (ARC).
- Extraneous line breaks are added for clarity
// First, we create a dictionary to hold our request parametersNSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:@"theSeanCook" forKey:@"screen_name"];
[params setObject:@"5" forKey:@"count"];
[params setObject:@"1" forKey:@"include_entities"];
[params setObject:@"1" forKey:@"include_rts"];
// Next, we create an URL that points to the target endpointNSURL *url =
[NSURL URLWithString:@"http://api.twitter.com/1/statuses/user_timeline.json"];
// Now we can create our request. Note that we are performing a GET request.TWRequest *request = [[TWRequest alloc] initWithURL:url
parameters:paramsrequestMethod:TWRequestMethodGET];
// Perform our request[request performRequestWithHandler:
^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (responseData) {
// Use the NSJSONSerialization class to parse the returned JSONNSError *jsonError;
NSArray *timeline =
[NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingMutableLeaveserror:&jsonError];
if (timeline) {
// We have an object that we can parseNSLog(@"%@", timeline);
}else {
// Inspect the contents of jsonErrorNSLog(@"%@", jsonError);
}}}];
Authenticated Requests
The real power of TWRequest is its handling of user authentication. In this example, we will first query the accounts that the user has stored on the device, then for the first account returned we retrieve the user's home timeline using GET statuses/home_timeline, which requires authentication. We will once again pass a parameter to the endpoint, include_rts, to illustrate how easy it is to do so with TWRequest.
- include_rts - 1
The equivalent HTTP request would be an OAuth-signed request of this format:
GET http://api.twitter.com/1/statuses/home_timeline.json?include_rts=1
To use TWRequest to make this authenticated request, we would do the following:
Source Code Notes:
- This example utilizes Automatic Reference Counting (ARC).
- Extraneous line breaks are added for clarity
// First, we need to obtain the account instance for the user's Twitter accountACAccountStore *store = [[ACAccountStore alloc] init];
ACAccountType *twitterAccountType =
[store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Request permission from the user to access the available Twitter accounts[store requestAccessToAccountsWithType:twitterAccountType
withCompletionHandler:^(BOOL granted, NSError *error) {
if (!granted) {
// The user rejected your requestNSLog(@"User rejected access to the account.");
}else {
// Grab the available accountsNSArray *twitterAccounts =
[store accountsWithAccountType:twitterAccountType];
if ([twitterAccounts count] > 0) {
// Use the first account for simplicityACAccount *account = [twitterAccounts objectAtIndex:0];
// Now make an authenticated request to our endpointNSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:@"1" forKey:@"include_entities"];
// The endpoint that we wish to callNSURL *url =
[NSURL
URLWithString:@"http://api.twitter.com/1/statuses/home_timeline.json"];
// Build the request with our parameterTWRequest *request =
[[TWRequest alloc] initWithURL:url
parameters:paramsrequestMethod:TWRequestMethodGET];
// Attach the account object to this request[request setAccount:account];
[request performRequestWithHandler:
^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (!responseData) {
// inspect the contents of errorNSLog(@"%@", error);
}else {
NSError *jsonError;
NSArray *timeline =
[NSJSONSerializationJSONObjectWithData:responseDataoptions:NSJSONReadingMutableLeaveserror:&jsonError];
if (timeline) {
// at this point, we have an object that we can parseNSLog(@"%@", timeline);
}else {
// inspect the contents of jsonErrorNSLog(@"%@", jsonError);
}}}];
} // if ([twitterAccounts count] > 0)
} // if (granted)
}];