Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inviting multiple friends using Facebook SDK in native iOS app

I am using the FB SDK to allow users to invite friends to download my app. I am creating a FB request when the user clicks an invite button. The action looks like this:

- (IBAction)inviteButtonPressed:(UIButton *)sender {
// create a dictionary for our dialog's parameters
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity: 7];

// set the frictionless requests parameter to "1"
[params setObject: @"1" forKey:@"frictionless"];
[params setObject: @"Test Invite" forKey:@"title"];
[params setObject:appID forKey:@"app_id"];


[params setObject: @"Test" forKey: @"message"];
if([friendsToInvite count] != 0){

    [params setObject:friendsToInvite forKey:@"to"];

    NSLog(@"%@", params);
}

// show the request dialog
[facebook dialog:@"apprequests" andParams:params andDelegate: nil];

}

The problem is I am passing an array of friends (selected by the user) for the object of the @"to" property. This is how the Facebook library tries to parse the @"to" object (code from Facebook):

        id fbid = [params objectForKey:@"to"];
        if (fbid != nil) {
            // if value parses as a json array expression get the list that way
            SBJsonParser *parser = [[[SBJsonParser alloc] init] autorelease];
            id fbids = [parser objectWithString:fbid];
            if (![fbids isKindOfClass:[NSArray class]]) {
                // otherwise seperate by commas (handles the singleton case too)
                fbids = [fbid componentsSeparatedByString:@","];
            }                
            invisible = [self isFrictionlessEnabledForRecipients:fbids];             
        }

My code is giving me this error:

-[__NSArrayM UTF8String]: unrecognized selector sent to instance 0x1aea00
 2012-05-08 01:48:29.958 shmob[2976:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM UTF8String]: unrecognized selector sent to instance 0x1aea00'

When I hardcode a single app id into the @"to" object, it works! Do you know how I can invite a list of Facebook friends?

like image 405
iosNoob Avatar asked Dec 28 '22 01:12

iosNoob


1 Answers

Found the fix:

I converted the array to a string using componentsjoinedbystring and then set the string as the param for the @"to" property. Like this:

if([friendsToInvite count] != 0){

    NSString * stringOfFriends = [friendsToInvite componentsJoinedByString:@","];

    [params setObject:stringOfFriends forKey:@"to"];

    NSLog(@"%@", params);
}

// show the request dialog
[facebook dialog:@"apprequests" andParams:params andDelegate: nil];

Works like a charm.

like image 70
iosNoob Avatar answered Jan 30 '23 02:01

iosNoob