Reputation: 3166
As a part of my project I need to show the list of tweets in iOS app. I tried with the below code but its returning the JSON. The same code is works fine with version 1. Now the twitter api version is 1.1 and one another warning I got that TWRequest is deprecated
. This deprecation is not the issue of this even I got same thing with SLRequest
. Here my problem is I need to fetch the JSON of Particular user tweets without authentication or with authentication
TWRequest *postequest=[[TWRequest alloc]initWithURL:[NSURL URLWithString:@"https://api.twitter.com/1.0/statuses/user_timeline.json?screen_name=coolCracker&count=2"] parameters:nil requestMethod:TWRequestMethodGET];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output;
if ([urlResponse statusCode]==200) {
NSError *err;
NSDictionary *PublicTimeline=[NSJSONSerialization JSONObjectWithData:responseData options:0 error:&err];
output=[NSString stringWithFormat:@"HTTP response status: %li\nPublic timeline:\n%@",(long)[urlResponse statusCode],PublicTimeline];
}
else
{
output=[NSString stringWithFormat:@"No feed HTTP response was: %li\n",(long)[urlResponse statusCode]];
}
[self performSelectorOnMainThread:@selector(displayResults:) withObject:output waitUntilDone:NO];
}];
-(void)displayResults:(NSString *)text
{
NSLog(@"tweets feed %@",text);
}
Upvotes: 4
Views: 1391
Reputation: 159
Moving forward the easiest way is adoption of Fabric.io from Tweeter as the development platform includes Tweeter embeds: https://get.fabric.io/native-social
"The easiest way to bring Twitter content into your app."
Honestly, I did not look into authentication part needed for Fabric.io.
Otherwise you need to follow and respect rules for accessing Twitter REST APIs or Streaming APIs, both well documented. OAuth authentication is required.
If you wish to avoid authentication in your app you can write a simple web REST API that will take care of authentication and serve (selected) tweets to your iOS app.
I personally prefer JavaScript on Node.js. Consider using libraries like express.js and oauth available on npm, but you can search npm for any other implemented Twitter proxy/API. The code essentially consists of getting an OAuth Access token and then calling selected twitter APIs, and respecting ratelimits by caching results.
Upvotes: -2
Reputation: 11201
I used this in my work and it worked pretty well. Use STTwitter. Download STTwitter files here.
Place this in your view didload:
STTwitterAPI *twitter = [STTwitterAPI twitterAPIAppOnlyWithConsumerKey:@"your consumer key"
consumerSecret:@"your secret key"];
[twitter verifyCredentialsWithSuccessBlock:^(NSString *bearerToken) {
[twitter getUserTimelineWithScreenName:@"your twitter account name"
successBlock:^(NSArray *statuses) {
self.twitterFeedList = [NSMutableArray arrayWithArray:statuses];
[self->listView reloadData];
} errorBlock:^(NSError *error) {
NSLog(@"%@", error.debugDescription);
}];
} errorBlock:^(NSError *error) {
NSLog(@"%@", error.debugDescription);
}];
Upvotes: 2
Reputation: 559
Hi Below code is working for me. Though TWRequest is deprecated it works for now. Later you can change it to SLRequest.
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *twitterType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Request access from the user to use their Twitter accounts.
[accountStore requestAccessToAccountsWithType:twitterType options:nil completion:^(BOOL granted, NSError *error) {
if (granted == YES){
//get accounts to use.
NSArray *accounts = [accountStore accountsWithAccountType:twitterType];
if ([accounts count] > 0) {
NSLog(@"account: %@", accounts);
ACAccount *loggedinaccount;
loggedinaccount = accounts[0];
// get tweets
TWRequest *postRequest=[[TWRequest alloc]initWithURL:[NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=HuntEthane&count=2"] parameters:nil requestMethod:TWRequestMethodGET];
[postRequest setAccount:loggedinaccount];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output;
if ([urlResponse statusCode]==200) {
NSError *err;
NSDictionary *PublicTimeline=[NSJSONSerialization JSONObjectWithData:responseData options:0 error:&err];
output=[NSString stringWithFormat:@"HTTP response status: %li\nPublic timeline:\n%@",(long)[urlResponse statusCode],PublicTimeline];
}
else
{
output=[NSString stringWithFormat:@"No feed HTTP response was: %li\n",(long)[urlResponse statusCode]];
}
[self performSelectorOnMainThread:@selector(displayResults:) withObject:output waitUntilDone:NO];
}];
}
}else{
// show alert to inser user_name and password to login on twitter
UIAlertView *alertTwitterAccount = [[UIAlertView alloc]
initWithTitle:@"Enter with your twitter account"
message:nil
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Login", nil];
[alertTwitterAccount setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
[[alertTwitterAccount textFieldAtIndex:0] setPlaceholder:@"User"];
[alertTwitterAccount setDelegate:self];
[alertTwitterAccount setTag:1];
[alertTwitterAccount show];
}
}];
Remember to add below frameworks to make this code work.
Security.FrameWork
If you want to use SLRequest
instead of TWRequest
, replace request statement with below statement
SLRequest *postRequest = [SLRequest
requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodGET
URL:[NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=HuntEthane&count=2"]parameters:nil];
Upvotes: 1