sapi
sapi

Reputation: 10224

How to tag users in a photo using the Facebook iOS SDK?

I can't seem to work out how to tag users in a Facebook photo upload.

The documentation seems to suggest that you use an array, but the following code doesn't parse correctly (causes an application crash)

- (void)uploadImage:(UIImage *)img
           withTags:(NSArray *)tags
{
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"msgstring", @"message",
                                   img, @"picture",  
                                   nil];
    if (tags) {
        [params setObject:tags
                   forKey:@"tags"];
    }

    self.requestType = FBAssistantRequestImageUpload;

    [self.facebook requestWithGraphPath:@"me/photos"
                              andParams:params
                          andHttpMethod:@"POST"
                            andDelegate:self];
}

It works fine without the tags. The array at the moment contains a single string with the identifier of the friend I wish to tag.

I assume I'm adding the tags incorrectly. I was hoping to avoid having to use the three-step method outlined here: Tag Friends in Facebook Photo Upload, as I believe that requires photos permission, which just posting the photo doesn't need.

Upvotes: 1

Views: 1055

Answers (1)

Fabio
Fabio

Reputation: 3067

here's the code I use to tag friends on photos:

NSMutableArray *tags = [[NSMutableArray alloc] init];
NSString *tag  = nil;
        if(self.selectedFriends != nil){
            for (NSDictionary *user in self.selectedFriends) {
                tag = [[NSString alloc] initWithFormat:@"{\"tag_uid\":\"%@\"}",[user objectForKey:@"id"] ];
                [tags addObject:tag];
            }
            NSString *friendIdsSeparation=[tags componentsJoinedByString:@","];
            NSString *friendIds = [[NSString alloc] initWithFormat:@"[%@]",friendIdsSeparation ];
    [params setObject:friendIds forKey:@"tags"];
    }

Upvotes: 2

Related Questions