Skip to content

Instantly share code, notes, and snippets.

@tamitutor
Last active May 6, 2016 08:03
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamitutor/c65c262d8343d433cf7f to your computer and use it in GitHub Desktop.
Save tamitutor/c65c262d8343d433cf7f to your computer and use it in GitHub Desktop.
Example iOS Facebook SDK 3.1* Graph Client
typedef void (^BooleanResultBlock)(BOOL succeeded, NSError *error);
typedef void (^ArrayResultBlock)(NSArray *objects, NSError *error);
typedef void (^IdResultBlock)(id object, NSError *error);
+ (id)sharedClient {
static FacebookGraphApiClient *__instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__instance = [[FacebookGraphApiClient alloc] init];
});
return __instance;
}
- (void)taggableFriendsWithArrayBlock:(ArrayResultBlock)block
{
/* make the API call */
[FBRequestConnection startWithGraphPath:@"/me/taggable_friends"
parameters:nil
HTTPMethod:@"GET"
completionHandler:
^(
FBRequestConnection *connection
, NSDictionary* result
, NSError *error
)
{
NSLog(@"%@", result);
NSArray* friends = [result objectForKey:@"data"];
block(friends, error);
}];
}
- (void)taggableFriendsDataWithIdBlock:(IdResultBlock)block
{
/* make the API call */
[FBRequestConnection startWithGraphPath:@"/me/taggable_friends"
parameters:nil
HTTPMethod:@"GET"
completionHandler:
^(
FBRequestConnection *connection
, NSDictionary* result
, NSError *error
)
{
NSLog(@"%@", result);
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithDictionary:@{@"friends": [result objectForKey:@"data"], @"paging": [result objectForKey:@"paging"]}];
block(dictionary, error);
}];
}
- (void)taggableFriendsPaginated:(NSString *)paginationUrl withIdBlock:(IdResultBlock)block
{
/* make the API call */
FBRequest *request = [[FBRequest alloc] initWithSession:FBSession.activeSession graphPath:nil];
FBRequestConnection *connection = [[FBRequestConnection alloc] init];
[connection addRequest:request completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithDictionary:@{@"friends": [result objectForKey:@"data"], @"paging": [result objectForKey:@"paging"]}];
NSLog(@"%@", dictionary);
block(dictionary, error);
}];
// Override the URL using the one passed back in 'next|previous'.
NSURL *url = [NSURL URLWithString:paginationUrl];
NSMutableURLRequest* urlRequest = [NSMutableURLRequest requestWithURL:url];
connection.urlRequest = urlRequest;
[connection start];
}
- (void)friendsListWithArrayBlock:(ArrayResultBlock)block
{
/* make the API call */
FBRequest* friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,
NSDictionary* result,
NSError *error) {
NSLog(@"%@", result);
NSArray* friends = [result objectForKey:@"data"];
NSLog(@"Found: %i friends", friends.count);
block(friends, error);
}];
}
- (void)searchUsersByName:(NSString *)name withArrayBlock:(ArrayResultBlock)block
{
NSDictionary *params = [[NSDictionary alloc] initWithObjectsAndKeys:name, @"q", @"user", @"type", nil];
/* make the API call */
[FBRequestConnection startWithGraphPath:@"/search" parameters:params HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSLog(@"%@", result);
NSArray *objects = [result objectForKey:@"data"];
block(objects, error);
}];
}
- (void)searchFriendsByNameFQL:(NSString *)name withArrayBlock:(ArrayResultBlock)block
{
NSString *query = [NSString stringWithFormat:@"select uid, name, pic_small from user where uid in (SELECT uid2 FROM friend WHERE uid1 = me()) and (strpos(lower(name),'%@')>=0 OR strpos(name,'%@')>=0)", name, name];
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
query, @"q",
nil];
/* make the API call */
[FBRequestConnection startWithGraphPath:@"/fql" parameters:params HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
NSLog(@"%@", result);
NSArray *objects = [result objectForKey:@"data"];
block(objects, error);
}];
}
- (void)friendsListFQLWithCursorPosition:(NSInteger)position withArrayBlock:(ArrayResultBlock)block
{
NSInteger secondPosition = kLimit + position;
NSString *query = [NSString stringWithFormat:@"select uid, name, pic_small from user where uid in (SELECT uid2 FROM friend WHERE uid1 = me()) LIMIT %i,%i", position, secondPosition];
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
query, @"q",
nil];
/* make the API call */
[FBRequestConnection startWithGraphPath:@"/fql" parameters:params HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
NSLog(@"%@", result);
NSArray *objects = [result objectForKey:@"data"];
block(objects, error);
}];
}
- (void)postStatusUpdate:(NSString *)statusUpdate withTaggableFriends:(NSArray *)taggableFriends withBooleanBlock:(BooleanResultBlock)block
{
// -- Note --
// "taggableFriends" array is simply an array of NSStrings of
// Facebook graph ids retrieved via, and distilled from,
// the "taggableFriendsWithArrayBlock" method
NSString *place = @"445911945529269"; // Real sample place
[FBRequestConnection startForPostStatusUpdate:statusUpdate place:place tags:taggableFriends completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
block(!error, error);
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment