使用swift从Facebook SDK GraphRequest获得进步

我用这段代码上传了一张图片:

FBSDKGraphRequest(graphPath: "me/photos", parameters: params as [NSObject : AnyObject], HTTPMethod: "POST").startWithCompletionHandler({ (connection, result, error) -> Void in if (error != nil) { print ("failed") } else { print ("success") } }) 

它到目前为止工作正常,但上传可能需要一段时间,我想告知用户当前的进度。 有没有办法获得目前的进展? 正如我在SDK文档中看到的那样,有一个FBSDKGraphRequestConnection ,它在requestConnection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:中提供了FBSDKGraphRequestConnectionDelegate及其进展内容requestConnection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:但是我无法理解,如何在我的小代码中实现这些东西。 有人可以帮忙吗?

我有其他人有同样的问题…谷歌没有找到任何用swift写的东西。 这是我的工作解决方案:

1)从NSObjectFBSDKGraphRequestConnectionDelegate派生你的类

2)写下你的上传过程:

  let uploadRequest = FBSDKGraphRequest(graphPath: "me/photos", parameters: params as [NSObject : AnyObject], HTTPMethod: "POST") let connection = FBSDKGraphRequestConnection() connection.delegate = self connection.addRequest(uploadRequest, completionHandler: { (connection, result, error) -> Void in if (error != nil) { // error handling } else { // success handling } }) connection.start() 

3)在你的类中实现委托,如下所示:

 func requestConnection(connection:FBSDKGraphRequestConnection, didSendBodyData bytesWritten:NSInteger, totalBytesWritten:NSInteger, totalBytesExpectedToWrite:NSInteger) { print("upload percent reached: " + String(Double(totalBytesWritten) / Double(totalBytesExpectedToWrite))) // handle your UI here to show the current status }