2015-09-02 146 views
0

我想檢查一下,如果我當前的Facebook令牌(它存在)仍然具有用戶授予的所有必需權限。解析Facebook SDK FBSDKGraphRequest結果

所以我所說的圖形檢索與我的令牌相關聯的所有已授予的權限:

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通過。

任何幫助?

回答

0

Got it!

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/