简体   繁体   中英

Facebook SDK. Invite friends to the app

I want to choose one of more friends and send them an invite to my app. I use invitable_friends method of Graph API to get a list of friends, who didn't install my app yet. Then I render my friends in the app UI. User choose friends and tap to send invitations. And here's the problem. I tried to use FBSDKAppInviteDialog , but I couldn't set specific users in its content. And the only one possibility to choose friends is to use facebook dialog, not custom view.

I tried to use FBSDKGameRequestDialog . Its content has a property to to set specific users, but the FBSDKGameRequestDialogDelegate methods are unreachable. And I'm not sure that using game request is the right way to invite friends to use my app.

Older Facebook SDK provided [FBWebDialogs presentRequestsDialogModallyWithSession:message:title:parameters:handler:] . And this worked fine.

I use it this way:

First, you get list of FB friends and store it in NSMutableArray:

- (void) getFriendsFromFB:(NSDictionary*)parameters invitable:(BOOL) invitable {
    NSString* graphPath = @"/me/friends";

    if (invitable) {
        graphPath = @"/me/invitable_friends";
    }

    FBSDKGraphRequest* request = [[FBSDKGraphRequest alloc] initWithGraphPath:graphPath parameters:parameters];
    [request startWithCompletionHandler:^(FBSDKGraphRequestConnection * connection, id result, NSError *error){
        if (error) {
            NSLog(@"Facebook: error handling friend invite request at path %@: %@", graphPath, error.description);
        }else{
            NSLog(@"Facebook: successfully handled friend request for graph path %@",graphPath);

            NSArray *friendArray = [result objectForKey:@"data"];

            for (NSDictionary* friendDict in friendArray) {

                FContact* contact = [[FContact alloc] initWithFaceBookID:friendDict[@"id"] name:friendDict[@"name"] avatarURL:friendDict[@"picture"][@"data"][@"url"]];
                contact.hasMyGameInstalled = !invitable;
                [[FStorage sharedInstance].friends addObject:contact];
            }

            NSLog(@"Facebook contacts received: %lu contacts",(unsigned long)friendArray.count);

            if (!invitable) {
                NSLog(@"Facebook: making game");
                storage.currentGame = [[FGame alloc] initWithPreset];
                [storage.savedGames addObject:storage.currentGame];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"GameInfoUpdated" object:nil];
            }
        }

    }];
}

Now, I can display list of FB friends and let user choose whom to invite. And when I have user selected, I'd propose a game to him:

- (void) proposeGameTo:(FContact*) contact{
    FBSDKGameRequestContent *gameRequestContent = [[FBSDKGameRequestContent alloc] init];

    // Look at FBSDKGameRequestContent for futher optional properties
    gameRequestContent.message = @"Let's play My brilliant game together";
    gameRequestContent.title = @"My custom invitation";
    gameRequestContent.to = @[contact.facebookID];
    gameRequestContent.actionType = FBSDKGameRequestActionTypeTurn;

    FBSDKGameRequestDialog* dialog = [[FBSDKGameRequestDialog alloc] init];
    dialog.frictionlessRequestsEnabled = YES;
    dialog.content = gameRequestContent;
    dialog.delegate = self;

    // Assuming self implements <FBSDKGameRequestDialogDelegate>
    [dialog show];
}

Finally I have found a solution of my problem. Using code like Dmitry Timoshenko has posted, FBSDKGameRequestDialog calls its dealloc , where it nilify his _delegate . That's why I didn't reach any FBSDKGameRequestDialogDelegate methods. I made a strong @property for FBSDKGameRequestDialog and now I receive facebook user ids in gameRequestDialog:didCompleteWithResults: .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM