Greg’s Online CS 193P Resources

May 5, 2009

Getting JSON and TwitterHelper working in Pres #2

Filed under: cs 193p — gregorykaiser @ 3:15 am
Tags:

I wrote a little Demo program to figure out JSON and the Twitter API. This may also help you get started. I did it as follows (assuming WordPress does not mangle it)

1. Start an Xcode “File>New Project” using the iPhone OS-“View-Based Application” as the template.

2. Drag these files from the “Presence2Files.zip” into the left pane
of your project into the “Classes: section” when you get the pop-up make
sure and check the “Copy” box.

TwitterHelper.h
TwitterHelper.m

(from the JSON Directory drag all 7 files also under “Classes” in your project)

JSON.h
NSObject+SBJSON.h
NSObject+SBJSON.m
NSString+SBJSON.h
NSString+SBJSON.m
SBJSON.h
SBJSON.m

3.Drag the TwitterUsers.plist file under your project’s “Resources”
in the left pane of Xcode.

4. In ViewController.m change the Twitter Host name by commenting out ifdef in +twitterHostName
<code>
+ (NSString *)twitterHostname
{
//#if USE_TEST_SERVER  //P2At
//	return @"www.stanford.edu/class/cs193p/presence-test";	//P2At
//#else	//P2At
	return @"twitter.com";
//#endif	//P2At
}

5.  In ViewController.m add where it will execute (I uncommented the the ViewController.m-viewDidLoad method and
put this stuff there

	//P2At
	NSDictionary *dict = [[NSDictionary alloc] initWithDictionary:[TwitterHelper  fetchInfoForUsername:@"stevewozniak"]];
	// Uncomment the next line to see the entire dictionary of User information
	//	NSLog(@"======>Twitter UserInfo for stevewozniak:%@", dict);
	NSString *displayname = [dict objectForKey:@"name"];
	NSLog(@"======>Twitter Name for stevewozniak:%@", displayname);
	NSString *picURL = [dict objectForKey:@"profile_image_url"];
	NSLog(@"======>Twitter Picture URL for stevewozniak:%@", picURL);
  	NSArray *userTimeline = [[NSArray alloc] initWithArray:[TwitterHelper  fetchTimelineForUsername:@"stevewozniak"]]; 
	// Uncomment the next line to see the entire array of User Timeline (its big)
//	NSLog(@"======>Twitter Timeline for stevewozniak:%@", userTimeline);
	for ( int i = 0 ; i< &#91;userTimeline count&#93;; i++) {
              NSLog(@"=============>Timeline  Array at index %d Status Msg:%@", i,             [[userTimeline objectAtIndex: i] objectForKey:@"text" ]);
	}
	[dict release];
	[userTimeline release];
	//P2At end

5. In ViewController.h add

#import “TwitterHelper.h”; //P2At

6. In Xcode do “Run>Console” so you can see NSLog msgs, hit “Build & Go” You will see in the Console window

Advertisements

2 Comments »

  1. […] […]

    Pingback by Stanford Uni Course CS193P - iPhone Dev SDK Forum — May 5, 2009 @ 5:10 am | Reply

  2. This helped me out tons! Thanks.

    Comment by Brian — May 29, 2009 @ 7:14 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: