Browse files

If we have an access token, use it to load a list of friends using th…

…e Facebook

Graph API and display them in the table view.
  • Loading branch information...
1 parent 04a3213 commit e350b52c87ebd9eaadef49f8e0a80b1b6a714fed @lukeredpath committed Jun 1, 2010
Showing with 51 additions and 6 deletions.
  1. +4 −1 Classes/LROAuth2DemoViewController.h
  2. +47 −5 Classes/LROAuth2DemoViewController.m
View
5 Classes/LROAuth2DemoViewController.h
@@ -7,15 +7,18 @@
//
#import <UIKit/UIKit.h>
+#import "ASIHTTPRequestDelegate.h"
@class LROAuth2AccessToken;
@class OAuthRequestController;
-@interface LROAuth2DemoViewController : UITableViewController {
+@interface LROAuth2DemoViewController : UITableViewController <ASIHTTPRequestDelegate> {
LROAuth2AccessToken *accessToken;
OAuthRequestController *oauthController;
+ NSArray *friends;
}
@property (nonatomic, retain) LROAuth2AccessToken *accessToken;
+@property (nonatomic, retain) NSArray *friends;
- (void)saveAccessTokenToDisk;
- (void)beginAuthorization;
View
52 Classes/LROAuth2DemoViewController.m
@@ -9,6 +9,9 @@
#import "LROAuth2DemoViewController.h"
#import "LROAuth2AccessToken.h"
#import "OAuthRequestController.h"
+#import "ASIHTTPRequest.h"
+#import "NSString+QueryString.h"
+#import "CJSONDeserializer.h"
NSString * AccessTokenSavePath() {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
@@ -18,6 +21,7 @@
@implementation LROAuth2DemoViewController
@synthesize accessToken;
+@synthesize friends;
- (void)viewDidLoad
{
@@ -42,11 +46,7 @@ - (void)viewDidAppear:(BOOL)animated
if (self.accessToken == nil) {
[self beginAuthorization];
} else {
- if ([self.accessToken hasExpired]) {
- //[self refreshAccessToken];
- } else {
- [self loadFacebookFriends];
- }
+ [self loadFacebookFriends];
}
}
@@ -99,7 +99,49 @@ - (void)refreshAccessToken;
- (void)loadFacebookFriends;
{
+ NSString *URLString = [NSString stringWithFormat:@"https://graph.facebook.com/me/friends?access_token=%@", [self.accessToken.accessToken stringByEscapingForURLQuery]];
+ NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:URLString]];
+
+ [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
+ [NSURLConnection connectionWithRequest:request delegate:self];
+}
+
+- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
+{
+ [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
+ NSError *jsonError = nil;
+ NSDictionary *friendsData = [[CJSONDeserializer deserializer] deserializeAsDictionary:data error:&jsonError];
+ if (jsonError) {
+ NSLog(@"JSON parse error: %@", jsonError);
+ } else {
+ self.friends = [friendsData valueForKey:@"data"];
+ [self.tableView reloadData];
+ }
+}
+
+#pragma mark -
+#pragma mark UITableView methods
+
+- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
+{
+ if (self.friends == nil) {
+ return 0;
+ }
+ return self.friends.count;
+}
+
+- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
+{
+ static NSString *identifier = @"Cell";
+
+ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
+ if (cell == nil) {
+ cell = [[[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:identifier] autorelease];
+ }
+ NSDictionary *friend = [self.friends objectAtIndex:indexPath.row];
+ cell.textLabel.text = [friend valueForKey:@"name"];
+ return cell;
}
@end

0 comments on commit e350b52

Please sign in to comment.