从swift的Facebook SDK GraphRequest获取进展 [英] Get progress from Facebook SDK GraphRequest with swift

查看:118
本文介绍了从swift的Facebook SDK GraphRequest获取进展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  FBSDKGraphRequest(graphPath:me / photos,参数:params作为[NSObject:AnyObject],HTTPMethod:POST)。startWithCompletionHandler({(connection,result,error) - > Void in 

if(error!= nil){
print (失败)
}
else {
print(success)
}

})

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

解决方案

我有其他人也有同样的问题... Google没有找到任何以swift写的东西。这是我的工作解决方案:



1)从 NSObject FBSDKGraphRequestConnectionDelegate写下你的上传过程,如下所示:



 code> let uploadRequest = FBSDKGraphRequest(graphPath:me / photos,参数:params as [NSObject:AnyObject],HTTPMethod:POST)
let connection = FBSDKGraphRequestConnection()
connection。 delegate = self
connection.addRequest(uploadRequest,completionHandler:{
(connection,result,error) - > Void in

if(error!= nil){
//错误处理
}
else {
//成功处理
}
})
connection.start()

3)在您的课堂上执行代理:

  func requestConnection(connection:FBSDKGraphRequestConnection,didSendBodyData bytesWritten:NSInteger,total BytesWritten:NSInteger,totalBytesExpectedToWrite:NSInteger){
print(upload percent reached:+ String(Double(totalBytesWritten)/ Double(totalBytesExpectedToWrite)))

//处理您的UI,显示当前状态
}


I upload an image with this pice of code:

    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")
        }

    })

It works fine so far, but the upload can take a while and I want to inform the user about the current progress. Is there any way to get the current progress? As I can see in the SDK docs, there is a FBSDKGraphRequestConnection that provides a FBSDKGraphRequestConnectionDelegate with progress stuff in requestConnection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:but I´m unable understand, how to implement this stuff in my little pice of code. Can anybody help?

解决方案

I someone else has the same problem ... Google does´t find anything written in swift. Here´s my working solution:

1) Derive your class from NSObject and FBSDKGraphRequestConnectionDelegate

2) Write your upload process like this:

    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) Implement the delegate in your class like this:

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
}

这篇关于从swift的Facebook SDK GraphRequest获取进展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆