Ios 无法在multipeer连接框架中显示数据传输进度
我正在尝试打印数据传输的进度,同时使用multipeer连接。Ios 无法在multipeer连接框架中显示数据传输进度,ios,swift,progress,multipeer-connectivity,Ios,Swift,Progress,Multipeer Connectivity,我正在尝试打印数据传输的进度,同时使用multipeer连接。 progress信息可在接收方的didStartReceivingResourceWithName方法中获得,在发送方的sendResource方法中获得。 以下是我如何实现接收器端: func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with pr
progress
信息可在接收方的didStartReceivingResourceWithName
方法中获得,在发送方的sendResource
方法中获得。
以下是我如何实现接收器端:
func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) {
DispatchQueue.main.async {
print (progress)
}
}
下面是我如何实现发送方端:
func sendFileAction()->Progress{
var filePath = Bundle.main.url(forResource: "10MO", withExtension: "file")
if mcSession.connectedPeers.count > 0 {
do {
let data = try Data(contentsOf: filePath!)
fileTransferProgressInSender = mcSession.sendResource(at: filePath!, withName: "filename", toPeer: mcSession.connectedPeers[0]) { (error) -> Void in
DispatchQueue.main.async {
if error != nil {
print("Sending error: \(String(describing: error))")
}else{
print("sendAFile with no error "+"filename")
}
}
}
}
catch let error as NSError {
let ac = UIAlertController(title: "Send file error", message: error.localizedDescription, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}
}
return(fileTransferProgressInSender)
}
接收器功能在开始时只显示一次进度
<NSProgress: 0x1c0133740> : Parent: 0x0 / Fraction completed: 0.0000 / Completed: 0 of 10485760
当打印
在控制台中显示实际进度时,进度条从0跳到1(在打印
之前达到1)
我做错了什么
再次感谢 对于接收方,您应该捕获要保存的进度变量,然后通过重复计时器查询它 苹果公司概述了这一趋势 可用于取消传输的NSProgress对象 或查询以确定转移进度
对于发件人,您将从函数中获得一个进度指示器作为返回。保存并使用计时器查询以查找状态。对于接收者,您应该捕获要保存的进度变量,然后通过重复计时器进行查询 苹果公司概述了这一趋势 可用于取消传输的NSProgress对象 或查询以确定转移进度
对于发件人,您将从函数中获得一个进度指示器作为返回。保存并用计时器查询以查找状态。谢谢@CodeBender的提示。事实上,我需要一个计时器。 我是这样做的:
func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) {
startTime = Date()
self.receptionProgress = progress
DispatchQueue.main.async {
self.receptionTimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.updateReceptionProgress), userInfo: nil, repeats: true)
self.receptionTimer.fire()
}
}
以及相应的功能:
@objc func updateReceptionProgress(){
self.receptionProgressBar.progress = Float(self.receptionProgress.fractionCompleted)
if self.receptionProgress.completedUnitCount >= self.receptionProgress.totalUnitCount{
self.receptionTimer.invalidate()
}
}
谢谢@CodeBender的提示。事实上,我需要一个计时器。 我是这样做的:
func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) {
startTime = Date()
self.receptionProgress = progress
DispatchQueue.main.async {
self.receptionTimer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.updateReceptionProgress), userInfo: nil, repeats: true)
self.receptionTimer.fire()
}
}
以及相应的功能:
@objc func updateReceptionProgress(){
self.receptionProgressBar.progress = Float(self.receptionProgress.fractionCompleted)
if self.receptionProgress.completedUnitCount >= self.receptionProgress.totalUnitCount{
self.receptionTimer.invalidate()
}
}
谢谢你的回答。但我不知道在哪里调用打印它的函数。我只有名为的
did完成接收资源和名为
的did开始接收资源,中间没有其他调用。谢谢你的回答。但我不知道在哪里调用打印它的函数。我只有名为
的DID FinishReceivingResourceWith和名为的DID StartReceivingResourceWith,中间没有调用任何其他内容。