ios - 如何在Google Drive API中获得下载进度(快速5)

标签 ios swift google-drive-api

我使用此功能从Google Drive API下载文件,并且希望获得下载进度。也许有人知道该怎么做?

func download(file: GTLRDrive_File) {
    let url = "https://www.googleapis.com/drive/v3/files/\(file.identifier!)?alt=media"
    let fetcher = drive.fetcherService.fetcher(withURLString: url)
    fetcher.beginFetch(completionHandler: { data, error in
        if let error = error {
            print(error.localizedDescription)
        }

        //Here I save data to the Documents
    })
}
我试图从fetcher.receivedProgressBlock获取它,但总是返回nil

最佳答案

解。实际适用于Swift 5:

func download(file: GTLRDrive_File) {
    let fileSize = file.size?.doubleValue //You need to fetch file size in your request q.fields = "kind,nextPageToken,files(mimeType,id,name,size)"
    let url = "https://www.googleapis.com/drive/v3/files/\(file.identifier!)?alt=media"
    let fetcher = drive.fetcherService.fetcher(withURLString: url)
    fetcher.beginFetch(completionHandler: { data, error in
        if let error = error {
            print(error.localizedDescription)
        }

        //Here I save data to the Documents
    })

    //Here you can get total bytes received (value is updated in real time)
    fetcher.receivedProgressBlock = { _, totalBytesReceived in
            guard let fileSize = fileSize else { return }
            let progress = Double(totalBytesReceived) / fileSize
            print(progress) //Here you can update UI (progress bar) or do something else
        }
}

关于ios - 如何在Google Drive API中获得下载进度(快速5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63937603/

相关文章:

swift - 如何为我的自定义 UIView 的属性设置动画?

ios - 无法将类型 'to_do_list.AddItemViewController' (0x1009f3970) 的值转换为 'to_do_list.ListDetailViewController' (0x1009f3850)

javascript - Google Drive NodeJS api返回已删除的文件

java - 如何解决这个 403 io 异常?

objective-c - Google Drive API - Objective C - 例子?

ios - 从 Xcode10 部署到 iOS11 设备时,ARWorldTrackingConfiguration 不起作用

ios - Xcode 只代表我的代码

ios - 将用户限制为一个小数点 UILabel Swift2

ios - 预期类型之前/之后 —>,Swift 3 中的预期声明

ios - 由于未捕获的异常 'NSInternalInconsistencyException' 终止应用程序,原因 : 'unexpected start state'