解析Facebook SDK FBSDKGraphRequest结果

时间:2015-09-02 18:53:39

标签: ios swift facebook-graph-api

我想检查一下,如果我当前的Facebook令牌(现有)仍然具有用户授予的所有必需权限。

所以我调用图表来检索与我的令牌相关的所有授权权限:

    FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "permissions"]).startWithCompletionHandler({ (connection, result, error) -> Void in
        var resultDictionary:NSDictionary!

        if (error != nil) {
            println("ERROR = \(error)")
        }
        else {
            resultDictionary = result as! NSDictionary
            println("1: ---- SUCCESS (result) ----")
            println("\(result)")

// this doesn´t work:
//                var test = resultDictionary.objectForKey("permissions")?.objectForKey("data")! ?? nil
//                var count = test.count
//                println("---- test (\(count))")
//                println(test)

        }
    })

Facebook的结果是:

{
id = 646571222102633;
permissions =     {
    data =         (
                    {
            permission = "user_friends";
            status = granted;
        },
                    {
            permission = "publish_actions";
            status = granted;
        },
                    {
            permission = "public_profile";
            status = granted;
        }
    );
};
}

因为可能没有/一个/很多权限,我想在权限/数据下遍历结果部分。

但是我不熟悉将这个NSDictionary转换成我可以count下面和iterate通过的元素。

任何帮助?

1 个答案:

答案 0 :(得分:0)

知道了!

    FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "permissions"]).startWithCompletionHandler({ (connection, result, error) -> Void in
        var resultDictionary:NSDictionary!

        if (error != nil) {
            DebugOutput("ERROR = \(error)")
        }
        else {
            resultDictionary = result as! [String: AnyObject]

            println("---- SUCCESS (result) ----")
            println("\(result)")

            var test = resultDictionary.objectForKey("permissions")?.objectForKey("data")! as! [[String:AnyObject]]
            var count = test.count
            println("---- test (\(count))")
            println(test)

        }
    })

非常感谢:http://ustwo.github.io/tech-blog/2014/08/01/ios-swift-dictionaries/