Created
July 25, 2012 21:30
-
-
Save ekoneil/3178821 to your computer and use it in GitHub Desktop.
Calling Facebook APIs with the 3.0 SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #1: Graph API: /me | |
- (void)requestMe { | |
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, | |
NSDictionary<FBGraphUser> *me, | |
NSError *error) { | |
if(error) { | |
[self printError:@"Error requesting /me" error:error]; | |
return; | |
} | |
NSLog(@"My name is %@", me.name); | |
}]; | |
} | |
// #2: Graph API: /me/friends | |
-(void)requestFriends { | |
[FBRequestConnection startForMyFriendsWithCompletionHandler:^(FBRequestConnection *connection, id data, NSError *error) { | |
if(error) { | |
[self printError:@"Error requesting /me/friends" error:error]; | |
return; | |
} | |
NSArray* friends = (NSArray*)[data data]; | |
NSLog(@"You have %d friends", [friends count]); | |
}]; | |
} | |
// #3: Graph API: /me/albums && /{albums[0]}/photos | |
-(void)requestAlbums { | |
[FBRequestConnection startWithGraphPath:@"/me/albums" | |
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) { | |
if(error) { | |
[self printError:@"Error requesting /me/albums" error:error]; | |
return; | |
} | |
NSArray* collection = (NSArray*)[result data]; | |
NSLog(@"You have %d albums", [collection count]); | |
NSDictionary* album = [collection objectAtIndex:0]; | |
NSLog(@"Album ID: %@", [album objectForKey:@"id"]); | |
// /albums[0]/photos | |
NSString* photos = [NSString stringWithFormat:@"%@/photos", [album objectForKey:@"id"]]; | |
[FBRequestConnection startWithGraphPath:photos | |
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) { | |
NSArray* photos = (NSArray*)[result data]; | |
NSLog(@"You have %d photo(s) in the album %@", | |
[photos count], | |
[album objectForKey:@"name"]); | |
}]; | |
}]; | |
} | |
// #4: Graph API: /me/groups | |
-(void)requestGroups { | |
[FBRequestConnection startWithGraphPath:@"/me/groups" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) { | |
if(error) { | |
[self printError:@"Error requesting /me/groups" error:error]; | |
return; | |
} | |
NSArray* collection = (NSArray*)[result data]; | |
NSLog(@"You have %d groups", [collection count]); | |
}]; | |
} | |
// #5: FQL via Graph API "SELECT uid, name, pic_square FROM user WHERE uid = me()" | |
-(void)requestMeWithFQL { | |
NSString* meFql = @"SELECT uid, name, pic_square FROM user WHERE uid = me()"; | |
[FBRequestConnection startWithGraphPath:@"/fql" | |
parameters:@{ @"q" : meFql } | |
HTTPMethod:@"GET" | |
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) { | |
if(error) { | |
[self printError:@"Error requesting me via FQL" error:error]; | |
return; | |
} | |
if([[result objectForKey:@"data"] count] > 0) { | |
// {data: [{user1}, {user2}]} | |
NSLog(@"FQL name: %@", [result objectForKey:@"data"][0][@"name"]); | |
} else { | |
NSLog(@"No users found"); | |
} | |
}]; | |
} | |
// #6: Graph API: batch request for /me, /me/groups, /me/albums | |
-(void)requestWithBatch { | |
FBRequestConnection* conn = [[FBRequestConnection alloc] init]; | |
[conn addRequest:[FBRequest requestForMe] completionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *me, NSError *error) { | |
if(error) { | |
[self printError:@"Error requesting /me via batch" error:error]; | |
return; | |
} | |
NSLog(@"Batched: My name is %@", me.name); | |
}]; | |
FBRequest* groups = [FBRequest requestForGraphPath:@"/me/groups"]; | |
[conn addRequest:groups completionHandler:^(FBRequestConnection *connection, id result, NSError *error) { | |
if(error) { | |
[self printError:@"Error requesting /me/groups via batch" error:error]; | |
return; | |
} | |
if([[result objectForKey:@"data"] count] > 0) { | |
NSLog(@"You have %d groups", [[result objectForKey:@"data"] count]); | |
} else { | |
NSLog(@"No groups returned"); | |
} | |
}]; | |
FBRequest* albums = [[FBRequest alloc] initWithSession:[FBSession activeSession] | |
graphPath:@"/me/albums" | |
parameters:@{@"fields": @"name,id"} | |
HTTPMethod:@"GET"]; | |
[conn addRequest:albums completionHandler:^(FBRequestConnection *connection, id result, NSError *error) { | |
if(error) { | |
[self printError:@"Error requesting /me/albums via batch" error:error]; | |
return; | |
} | |
NSArray* collection = (NSArray*)[result data]; | |
NSLog(@"You have %d albums", [collection count]); | |
}]; | |
[conn start]; | |
} | |
// #7: Graph API: post a status. | |
-(void)postStatusAsHelper { | |
[FBRequestConnection startForPostStatusUpdate:@"Hello World?" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) { | |
if(error) { | |
[self printError:@"Error posting status using static" error:error]; | |
return; | |
} | |
NSLog(@"Post ID: %@", result[@"id"]); | |
}]; | |
} | |
// #8: Graph API: post an Open Graph action | |
-(void)postAction { | |
id<BirdSightingAction> action = (id<BirdSightingAction>)[FBGraphObject graphObject]; | |
action.bird = @"http://samples.ogp.me/377812905594754"; | |
FBRequest* newAction = [[FBRequest alloc]initForPostWithSession:[FBSession activeSession] graphPath:@"/me/birdsighting:see" graphObject:action]; | |
FBRequestConnection* conn = [[FBRequestConnection alloc] init]; | |
[conn addRequest:newAction completionHandler:^(FBRequestConnection *connection, id result, NSError *error) { | |
if(error) { | |
[self printError:@"Error posting action" error:error]; | |
return; | |
} | |
NSLog(@"Bird sighting action ID: %@", result[@"id"]); | |
}]; | |
[conn start]; | |
} | |
// #9: FQL via Graph API | |
-(void)requestFriendsWithFQL { | |
NSString* fql = | |
@"{" | |
@"'allfriends':'SELECT uid2 FROM friend WHERE uid1=me()'," | |
@"'frienddetails':'SELECT id, name, url, pic FROM profile WHERE id in (SELECT uid2 FROM #allfriends)'," | |
@"}"; | |
[FBRequestConnection startWithGraphPath:@"/fql" | |
parameters:@{ @"q" : fql} | |
HTTPMethod:@"GET" | |
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) { | |
if(error) { | |
[self printError:@"Error reading friends via FQL" error:error]; | |
return; | |
} | |
NSMutableArray* friends = [[NSMutableArray alloc]init]; | |
NSArray* friendIds = ((NSArray*)[result data])[0][@"fql_result_set"]; | |
NSArray* friendInfo = ((NSArray*)[result data])[1][@"fql_result_set"]; | |
for(int i = 0; i < [friendIds count]; i++) { | |
NSDictionary* friend = [[NSDictionary alloc]initWithObjectsAndKeys: | |
[[friendInfo objectAtIndex:i] objectForKey:@"id"], @"id", | |
[[friendInfo objectAtIndex:i] objectForKey:@"name"], @"name", | |
[[friendInfo objectAtIndex:i] objectForKey:@"pic"], @"pic", | |
[[friendInfo objectAtIndex:i] objectForKey:@"url"], @"url", nil]; | |
[friends addObject:friend]; | |
} | |
int count = [friends count]; | |
NSLog(@"You have %d friends", count); | |
for(int i = 0; i < [friends count]; i++) { | |
NSLog(@"Name: %@", [[friends objectAtIndex:i] objectForKey:@"name"]); | |
} | |
}]; | |
} | |
// #10: Shared error handler | |
-(void)printError:(NSString*)message error:(NSError*)error { | |
if(message) { | |
NSLog(@"%@", message); | |
} | |
// works for 1 FBRequest per FBRequestConnection | |
int userInfoCode = [error.userInfo[@"com.facebook.FBiOSSDK:ParsedJSONResponseKey"][0][@"body"][@"error"][@"code"] integerValue]; | |
NSString* userInfoMessage = error.userInfo[@"com.facebook.FBiOSSDK:ParsedJSONResponseKey"][0][@"body"][@"error"][@"message"]; | |
// works for batches of > 1 FBRequest per FBRequestConnection | |
// int userInfoCode = [error.userInfo[@"com.facebook.FBiOSSDK:ParsedJSONResponseKey"][@"body"][@"error"][@"code"] integerValue]; | |
// NSString* userInfoMessage = error.userInfo[@"com.facebook.FBiOSSDK:ParsedJSONResponseKey"][@"body"][@"error"][@"message"]; | |
// outer error | |
NSLog(@"Error: %@", error); | |
NSLog(@"Error code: %d", error.code); | |
NSLog(@"Error message: %@", error.localizedDescription); | |
// inner error | |
NSLog(@"Error code: %d", userInfoCode); | |
NSLog(@"Error message: %@", userInfoMessage); | |
if(userInfoCode == 2500) { | |
UIAlertView* view = [[UIAlertView alloc]initWithTitle:@"Facebook" | |
message:@"You're not logged in." | |
delegate:nil | |
cancelButtonTitle:@"OK" | |
otherButtonTitles:nil]; | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
[view show]; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the call get albums with permision "user_photos" returns nil always. does anyone got this working? or it is still an sdk bug?