使用 swift 从 Facebook SDK GraphRequest 获取进度
Get progress from Facebook SDK GraphRequest with swift
我用这段代码上传图片:
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
但我无法理解,如何在我的小程序中实现这些内容的代码。有人可以帮忙吗?
我其他人也有同样的问题...Google 没有找到任何用 swift 写的东西。这是我的工作解决方案:
1) 从 NSObject
和 FBSDKGraphRequestConnectionDelegate
中导出 class
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) 像这样在 class 中实现委托:
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
}
我用这段代码上传图片:
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
但我无法理解,如何在我的小程序中实现这些内容的代码。有人可以帮忙吗?
我其他人也有同样的问题...Google 没有找到任何用 swift 写的东西。这是我的工作解决方案:
1) 从 NSObject
和 FBSDKGraphRequestConnectionDelegate
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) 像这样在 class 中实现委托:
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
}