Search code examples
iosswiftfacebookfacebook-ios-sdk

Get only new friends from Facebook iOS


Our application uses Firebase. For authenification in the application, we use Facebook. With the help of the Facebook SDK I get the user's friends The question is, can I only ask for "new friends"? For example, I requested a list of all friends on October 25, and to not ask the list of all friends of the user to be next time, I send timestamp parameters from when I need information about user's friends. I read a lot of posts and searched for information, but did not find anything useful in my situation. Thank you.

func getFriends() {
    DispatchQueue.global(qos: .background).async {
        let params = ["fields": "id, first_name, last_name, name, email, picture"]
        let graphRequest = FBSDKGraphRequest(graphPath: "/me/friends", parameters: params)
        let connection = FBSDKGraphRequestConnection()
        connection.add(graphRequest, completionHandler: { (connection, result, error) in
            if error == nil {
                guard let userData = result as? [String : Any] else { return }
                guard let jsonData = userData["data"] as? [[String : Any]] else { return }
                debugPrint("facebook data", jsonData)
                do {
                    let data = try JSONSerialization.data(withJSONObject: jsonData, options: JSONSerialization.WritingOptions.prettyPrinted)
                    let jsonDecoder = JSONDecoder()
                    let friends = try jsonDecoder.decode(Array<FacebookFriendModel>.self, from: data)
                    debugPrint("friends", friends.count)
                    self.saveFriends(friends)
                } catch {
                    debugPrint(error.localizedDescription)
                }
            } else {
                print("Error Getting Friends \(error?.localizedDescription ?? "")");
            }
        })

        connection.start()
    }
}

Solution

  • The question is, can I only ask for "new friends"? For example, I requested a list of all friends on October 25, and to not ask the list of all friends of the user to be next time, I send timestamp parameters from when I need information about user's friends.

    No, that kind of functionality is simply not available.

    Facebook deliberately does not provide the information when two users became friends.