Search code examples
iosobjective-cfacebookfacebook-graph-apifacebook-ios-sdk

How to tag friends in startWithGraphPath Facebook iOS


I can't seem to get my posts to work using the Facebook SDK for iOS. I am using the "startWithGraphPath:parameters:HTTPMethod:completionHandler:" function. I can get the message, place, and picture working but I can't seem to tag friends. I have tried setting the NSDictionary key and object to

parameters[@"tags"] = self.friendsIDs; 

which is a string of comma separated Facebook User ID's (per the Facebook reference page: https://developers.facebook.com/docs/reference/api/user/#posts). However this always comes up:

com.facebook.sdk:ParsedJSONResponseKey={
    body =     {
        error =         {
            code = 100;
            message = "(#100) param tags must be an array.";
            type = OAuthException;

So then I put them in an array and it still didn't work. In fact then I get a different error:

-[__NSArrayM length]: unrecognized selector sent to instance 0x1228abc0 2013-09-26 16:47:36.917 DrinkingGamePicker[27558:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM length]: unrecognized selector sent to instance 0x1228abc0sent to instance 0x1228abc0'

Anyone know how to format these parameters? I can't find any documentation or clear examples. Thanks in advance.

As a side note, does anyone know how to post from an app and tag people but not a place? I feel like every method means you have to at least tag a place first.

Thanks!


Solution

  • From my experience, you can't tag friends with the post at the same time. However you can tag friends after the post is success. After posting, you should get the post id from the completionHandler block. Here is the code what i am using right now.

    NSString *postId = //result from posting completionHandler;
    NSString *path = [NSString stringWithFormat:@"%@/tags",postId];
    NSMutableArray *tags = [NSMutableArray array];
    for (NSString *friendId in self.tagFriends) {
        [tags addObject:[NSDictionary dictionaryWithObject:friendId forKey:@"tag_uid"]];
    }
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:JSONStringWithObject(tags) forKey:@"tags"]; 
    //JSONStringWithObject() is just a function develop by myself using NSJSONSerialization to wrap NSArray into JSON format NSString
    
    [FBRequestConnection startWithGraphPath:path parameters:param HTTPMethod:@"POST"     completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
        //check error here 
    }];