解析Facebook好友列表

时间:2015-02-17 15:21:16

标签: xamarin.ios xamarin

直到统一的api,我用这段代码来解析Facebook的好友列表:

var list = new List<FacebookFriend>();
var graphObject = result as FBGraphObject;

using (var array = (NSMutableArray)graphObject[new NSString("data")])
{
    for (uint i = 0; i < array.Count; i++)
    {
        using (var friend = new NSDictionary(array.ValueAt (i)))
        {
            friends.Add (new FacebookFriend
            {
                ID = friend.ObjectForKey (new NSString ("uid")).ToString (),
                Name = friend.ObjectForKey (new NSString ("name")).ToString (),
            });
        }
    }
}

FacebookFriends:

public class FacebookFriend
    {

        public string ID { get; set; }
        public string name { get; set; }

    }

数组如下:

数组{({id = 100009010208340; name =&#34; Patricia Amijajbjhcdj Wongsky&#34 ;;},{id = 100009036398265; name =&#34; Patricia Amijcfcihbfe Vijayvergiyasen&#34 ;;},{id = 100009086915834 ;姓名=&#34; Elizabeth Amijhfiaehcd Bharambesky&#34 ;;})}

但是现在NSDictionary的保护级别发生了变化,代码中断了。 我尝试使用一些工厂方法,但没有成功。

有没有人知道在这种情况下应该使用什么而不是

new NSDictionary(array.ValueAt (i)))

由于

2 个答案:

答案 0 :(得分:0)

应该是这样的:

Runtime.GetNSObject<NSDictionary> (array.ValueAt (i));

附注:在循环内创建相同的NSString常量效率不高。你应该做点什么:

using (var uid = new NSString ("uid"))
using (var name = new NSString ("name")) {
    for (uint i = 0; i < array.Count; i++) {
       using (var friend = Runtime.GetNSObject<NSDictionary> (array.ValueAt (i))) {
        friends.Add (new FacebookFriend
        {
            ID = friend.ObjectForKey (uid).ToString (),
            Name = friend.ObjectForKey (name).ToString (),
        });
    }
}

答案 1 :(得分:0)

我已经编译了答案并添加了一个可能对某人有帮助的内容,因为我花了几天时间才弄清楚如何在Xamarin IOS中对统一API和Facebook SDK V2.2进行此请求。

List<string> mUserFriendsList = new List<string>();
    if(FBSession.ActiveSession.IsOpen)
                    {
                        var dic = new NSDictionary();
                        FBRequestConnection.StartWithGraphPath("me/friends?fields=id",dic,"GET",(connection,result,error) =>
                        {
                            if(error != null){
                                Console.WriteLine(error.Description);
                                InvokeOnMainThread (() => new UIAlertView ("Error!", "An error has occured while trying to connect to facebook. Please try again later.", null, "Ok", null).Show ());
                            }else{
                                var graphObject = result as FBGraphObject;
                                using (var array = (NSMutableArray)graphObject[new NSString("data")])
                                {
                                     using(var id = new NSString("id"));
                                     for (uint i = 0; i < array.Count; i++) 
                                     {
                                        using (var friend = Runtime.GetNSObject<NSDictionary> (array.ValueAt (i))) 
                                        {
                                            mUserFriend = friend["id"].ToString ();
                                            mUserFriendsList.Add (mUserFriend);
                                        }
                                    }
                                }
                            }
                        });
                    }