【问题标题】:Facebook SDK. Invite friends to the app脸书 SDK。邀请朋友加入应用程序
【发布时间】:2015-04-07 15:44:50
【问题描述】:

我想从更多朋友中选择一个并向他们发送我的应用程序的邀请。 我使用 Graph API 的invitable_friends 方法来获取尚未安装我的应用程序的朋友列表。然后我在应用程序 UI 中渲染我的朋友。用户选择朋友并点击发送邀请。 这就是问题所在。 我尝试使用FBSDKAppInviteDialog,但无法在其内容中设置特定用户。选择朋友的唯一一种可能性是使用 facebook 对话框,而不是自定义视图。

我尝试使用FBSDKGameRequestDialog。它的内容有一个属性to 来设置特定的用户,但是FBSDKGameRequestDialogDelegate 方法是无法访问的。而且我不确定使用游戏请求是否是邀请朋友使用我的应用的正确方式。

旧版 Facebook SDK 提供 [FBWebDialogs presentRequestsDialogModallyWithSession:message:title:parameters:handler:]。这很好用。

【问题讨论】:

    标签: ios objective-c iphone facebook-graph-api facebook-ios-sdk


    【解决方案1】:

    我是这样用的:

    首先,获取 FB 好友列表并将其存储在 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];
                }
            }
    
        }];
    }
    

    现在,我可以显示 FB 好友列表并让用户选择要邀请的人。 当我选择了用户后,我会向他推荐一款游戏:

    - (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];
    }
    

    【讨论】:

    • 您是否达到了委托方法调用?我——不。所以我既无法获得用户 ID(不是邀请令牌)也无法获得请求 ID
    • 您指的是哪些委托方法?你真的不需要委托来获取用户 ID,它只是一个带有结果块的调用,就是这样。
    • 在临时invite_tokens 和常量user_ids 之间有所区别。 invitable_friendsid 字段中返回 invite_tokens。它是一个字母数字字符串。 FBSDKGameRequestDialogDelegate 的方法 gameRequestDialog:didCompleteWithResults: 返回 user_ids - 数字用户标识符。
    • 我试过你的代码来邀请我的朋友,它成功了。但我的问题是,只有一部分被邀请的玩家会收到通知。我可以做什么或检查什么?
    • 现在看来这取决于可能允许/拒绝邀请的确切人员的 FB 设置。
    【解决方案2】:

    我终于找到了解决问题的方法。使用像 Dmitry Timoshenko 发布的代码,FBSDKGameRequestDialog 调用它的dealloc,它消除了他的_delegate。这就是为什么我没有达到任何FBSDKGameRequestDialogDelegate 方法的原因。 我为FBSDKGameRequestDialog 做了一个强大的@property,现在我在gameRequestDialog:didCompleteWithResults: 中收到了facebook 用户ID。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-26
      • 1970-01-01
      • 2015-07-29
      • 2012-08-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多