使用 SWIFT 在 iOS SDK for Facebook 上使用 FBSDKGraphRequest
Posted
技术标签:
【中文标题】使用 SWIFT 在 iOS SDK for Facebook 上使用 FBSDKGraphRequest【英文标题】:Working with FBSDKGraphRequest on iOS SDK for Facebook with SWIFT 【发布时间】:2015-10-13 14:38:24 【问题描述】:我对 ios Facebook SDK 还是很陌生,而且速度很快,但遇到了一个小问题。
我想从 Facebook 用户那里获取个人资料信息,例如姓名、生日和音乐。
我尝试关注姓名和生日,但不知道如何遍历 NSDictionary。最后,我想将所有艺术家/乐队名称保存在名为 allMusic 的字符串变量中
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphpath: "me?fields=id,name,email,bio,birthday,hometown,gender,music,events", parameters: nil)
graphRequest.startWithCompletionHandler( (connection, result, error) -> Void in
if ((error) != nil)
// Process error
print("Error: \(error)")
else
let userName : NSString = result.valueForKey("name") as! NSString
let userEmail : NSString = result.valueForKey("email") as! NSString
let bday : NSString = result.valueForKey("birthday") as! NSString
let bio : NSString = result.valueForKey("bio") as! NSString
let id : NSString = result.valueForKey("id") as! NSString
let music: NSDictionary = result.valueForKey("music") as! NSDictionary
var allMusic: String = ""
for val in music
print(val.valueForKey("data"))
// allMusic += "----Value---"
)
如果我打印(音乐),这会出现在命令行上
data = (
"created_time" = "2015-07-19T12:17:26+0000";
id = 345946458834143;
name = Wanda;
,
"created_time" = "2015-06-23T01:20:04+0000";
id = 119490691442848;
name = Knorkator;
... and so on
"created_time" = "2013-02-11T19:13:08+0000";
id = 8811047260;
name = "Infected Mushroom";
);
paging =
cursors =
after = ODgxMTA0NzI2MAZDZD;
before = MzQ1OTQ2NDU4ODM0MTQz;
;
next = "some url...";
;
【问题讨论】:
【参考方案1】:let array = ((result as! NSDictionary).value(forKey: "data") as! NSArray)
for myData in array
let music = ((myData as! NSDictionary).value(forKey: "name")) as! String //you can also save this to global variable :)
希望这会奏效,伙计
【讨论】:
以上是关于使用 SWIFT 在 iOS SDK for Facebook 上使用 FBSDKGraphRequest的主要内容,如果未能解决你的问题,请参考以下文章
例外:Google Maps SDK for iOS 必须在使用前通过 [GMSServices provideAPIKey:...] 进行初始化