Twitter tweets according to the screen name in iPhone using Twitter 1.1 api
1. Login with your twitter credentials https://dev.twitter.com/ through this link .
2. Now on the top right corner your profile pic is there and click on that and go to my applications and create a new application.
3. Create a new application
4.Fill the fields and ignore the call back url
5. Here you get the consumer key and the consumer secret.
6.Create your own access token
here you can get your own access token
7.Concatenate the consumer key , a colon character ":", and the consumer secret in to a single string.
example
Consumer Key xvz1evFS4wEEPTGEFPHBog
Consumer Secret L8qq9PZyRg6ieKGEKhZolGC0vJWLw8iEJ88DRdyOg
Concatenate these two xvz1evFS4wEEPTGEFPHBog:L8qq9PZyRg6ieKGEKhZolGC0vJWLw8iEJ88DRdyOg
8.Now the concatenate string is to be encoded with the base 64 using the below link,
http://www.motobit.com/util/base64-decoder-encoder.asp
convert the source data.
At last the encoded base 64 string is eHZ6MWV2RlM0d0VFUFRHRUZQSEJvZzpMOHFxOVBaeVJnNmllS0dFS2hab2xHQzB2SldMdzhpRUo4OERSZHlPZw==
9.Now open your xcode and create an new project and in this method
create propertys and synthesize these parameters
NSMutableData *responseData;
NSString *acesToken;
NSString *compareString;
NSArray *tweetsArray;
NSDictionary *tweetsDictionary;
IBOutlet UIImageView *imag;
-(void)viewdidload
{
compareString =@"accestoken";
NSString* encodeingStr = @"eHZ6MWV2RlM0d0VFUFRHRUZQSEJvZzpMOHFxOVBaeVJnNmllS0dFS2hab2xHQzB2SldMdzhpRUo4OERSZHlPZw==";
NSData* data = [encodeingStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *serverURL = @"https://api.twitter.com/oauth2/token";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:serverURL]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:data];
[request addValue:[NSString stringWithFormat:@"Basic %@",encodeingStr] forHTTPHeaderField:@"Authorization"];
[request addValue:@"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
NSString *post =@"grant_type=client_credentials";
[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[conn start];
}
this is how we are requesting the twitter api 1.1.
Now the delegate methods will fire,
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
responseData = [[NSMutableData alloc]init];
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *status = [[NSString alloc] initWithData:responseData
encoding:NSUTF8StringEncoding];
tweetsDictionary = [self dictionaryFromJSONData:[status dataUsingEncoding:NSUTF8StringEncoding]];
if ([comparestring isEqualToString:@"accestoken"])
{
acestoken = [tweetsDictionary objectForKey:@"access_token"];
NSLog(@"the data in the accesstoken is %@",acestoken);
[self postingfortweetes];
}
else if ([comparestring isEqualToString:@"gettingtweets"])
{
self.twittertable.dataSource = self;
NSLog(@"the data in the status is %@",status);
[twittertable reloadData];
NSArray *arr = [[tweetsDictionary valueForKey:@"user"]objectAtIndex:0];
NSLog(@"the data in the user is%@",arr);
NSString *str = [arr valueForKey:@"profile_image_url_https"];
NSLog(@"the data in the url is %@",str);
NSURL *url = [NSURL URLWithString:str];
NSLog(@"the url is %@",url);
NSData *data = [NSData dataWithContentsOfURL:url];
imag.image = [UIImage imageWithData:data];
}
}
-(void)postingfortweetes
{
comparestring = @"gettingtweets";
NSString *serverURL =[NSString stringWithFormat:@"https://api.twitter.com//1.1/statuses/user_timeline.json?count=100&screen_name=%@",twitterid];
//here in the place of twitter id give your twitter id
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:serverURL]];
[request setHTTPMethod:@"GET"];
[request addValue:[NSString stringWithFormat:@"Bearer %@",acestoken] forHTTPHeaderField:@"Authorization"];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[conn start];
}
- (NSDictionary *)dictionaryFromJSONData:(NSData *)data
{
NSError *error;
NSDictionary *dictionaryParsed = [NSJSONSerialization JSONObjectWithData:data
options:0
error:&error];
if (!dictionaryParsed)
{
if (error)
{
//#warning Handle the error here.
NSLog(@"error");
}
return nil;
}
return dictionaryParsed;
}
Now the total tweets are in tweetsDictionary, now these tweets to the uitableview.
Drag and drop your UITableview in your xib file and connect delegate method and the data source method .
// uitableview delegate methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[tweetsDictionary valueForKey:@"text"] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [tweetsDictionary valueForKey:@"text"][indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 90;
}
sorry for my bad english, i hope this tutorial will help you .