【问题标题】:How to enable user to add friends via friend's username and display user's friend list in tableview with swift如何使用户能够通过朋友的用户名添加朋友并使用swift在tableview中显示用户的朋友列表
【发布时间】:2015-05-29 00:45:23
【问题描述】:

我目前正在使用 Swift 开发 iOS 应用程序,用户可以通过用户名在一个视图中添加朋友,并在另一个表视图中显示用户的朋友列表,我目前正在使用解析,我可以让我的应用程序让用户签名启动并登录。

我想知道通过用户名添加好友的代码,并通过解析显示用户的好友列表, 我已尝试寻找此解决方案,但除了如何从 Facebook 获取与我的应用程序无关的朋友列表外,我一无所获。

感谢任何帮助,如果您需要任何其他信息,请告诉我! (对不起我的英语不好)。

【问题讨论】:

    标签: ios xcode uitableview swift


    【解决方案1】:

    您需要使用适用于 iOS 的 FBSDK 来为 Facebook 好友列表发出图形请求。

    我不太明白您是否要添加已在您的应用中注册或拥有 Facebook 帐户的“朋友”,但无论如何您都需要将您的用户存储在数据存储中,我相信 Parse 有 PFObject ,你可以这样保存:

    var appUser = PFObject(className:"AppUser")
    appUser["userFullName"] = "John Doe"
    appUser["userFacebookID"] = 1
    appUser["userEmail"] = "j.doe@doe.com"
    appUser.saveInBackgroundWithBlock {
      (success: Bool, error: NSError?) -> Void in
      if (success) {
        // The object has been saved.
      } else {
        // There was a problem, check error.description
      }
    }
    

    从那里您需要将用户与“友谊”对象相关联,例如:

    var userFriendship = PFObject(className:"Friendship")
        appUser["invitedUserEmail"] = "jane.doe@doe.com"
        appUser["invitingUserEmail"] = "j.doe@doe.com"
        appUser["invitationStatus"] = "pending"
        appUser.saveInBackgroundWithBlock {
          (success: Bool, error: NSError?) -> Void in
          if (success) {
            // The object has been saved.
          } else {
            // There was a problem, check error.description
          }
        }
    

    之后您可以更新对象以将邀请状态更改为“接受”或“拒绝”、“取消”等。

    要获取用户邀请和好友列表,您需要使用您正在寻找的参数进行 ParseQuery,例如:

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"invitingUserEmail = 'j.doe@doe.com' AND invitationStatus = 'accepted'"];
    PFQuery *query = [PFQuery queryWithClassName:@"Friendship" predicate:predicate];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
      if (!error) {
        // The find succeeded.
        NSLog(@"Successfully retrieved %d friends.", objects.count);
        // Do something with the found objects
        for (PFObject *object in objects) {
            NSLog(@"%@", object.objectId);
        }
      } else {
        // Log details of the failure
        NSLog(@"Error: %@ %@", error, [error userInfo]);
      }
    }];
    

    您可以在此处https://parse.com/docs/ios/guide 的 Parse 文档中找到更多信息

    【讨论】:

    • 是的,我希望用户添加已经在我的应用中注册的其他用户,而我的应用根本不包含 Facebook。您能否详细解释一下您在添加其他用户并将其保存在用户中的代码目的。非常感谢!。
    • 现在我明白了更多,但我有点困惑如何将此代码链接到添加用户并显示用户的朋友列表。 (很抱歉没有在顶部评论中包含此内容)
    猜你喜欢
    • 1970-01-01
    • 2012-02-12
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多