Twitter + iPhone = MyTweet
I just started playing with Twitter API and was interested to see some good API’s in objective-c. The one i started to look at is created by Matt Legend
You can see his stuff here
Anyway after simply checking(svn co) out his repository, I was able to quickly build a simple client very quickly
Here is version 0.000001 alpha
Matt nicely uses the standard way of doing things in objetive c with async calls
- Delegate - call back mechanism
Your View Controller or what ever has to simply implement the protocol MGTwitterEngineDelegate and you are getting responses from Twitter :-)
Here is my code
twitterEngine=[[MGTwitterEngine alloc] initWithDelegate:self];
[twitterEngine setUsername:@"yourusername" password:@"yourpassword"];
[twitterEngine getRepliesStartingAtPage:0];
And just implement the callback to get the responses. All responses are nicely packed within an NSArray. Each element is a NSDictionary.. real nice. Oh and for testing NSLog will just spit everything out for your convenience
Here is my code to show the results in a TableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(data==nil)
{
NSLog(@"Returning 0");
return 0;
}
else
{
NSLog(@"Returning count %d",[data count]);
return [data count];
}
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *txt=@"Nothing..";
if(data!=nil)
{
NSDictionary *dict=(NSDictionary*)[data objectAtIndex:indexPath.row];
txt=[dict objectForKey:@"text"];
}
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.text=txt;
}
// Set up the cell...
return cell;
}
- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)identifier{
//NSLog(@"Satuses Received %@:%@",identifier,statuses);
if(data!=nil)
{
[data release];
}
data=[[NSMutableArray arrayWithArray:statuses] retain];
UITableView *tableView=(UITableView*)self.view;
[tableView reloadData];
NSLog(@"Reloading data");
}
The idea is to build a good, free version for the iPhone.