在 iOS 11 上,我无法使用Multipeer Connectivity发送 轻量级文件。
但是,在 iOS 10 上,无论大小如何,它都能够发送。
如果无法发送,则不调用“MCSession.sendResource(at:withName:toPeer:withCompletionHandler :)”的“completionHandler”中的块。
(在接收方,未调用“MCSessionDelegate”的“session(_:didFinishReceivingResourceWithName:fromPeer:at:withError :)”。)
也许,当尺寸很小时,传输状态的处理似乎无法正常工作。
即使是iOS 11,也能够接收轻量级的文件。
在iOS 11上发送轻量级文件需要什么?
import MultipeerConnectivity
class SessionContainer: NSObject, MCSessionDelegate {
var session:MCSession?
var delegate:SessionContainerDelegate?
func sendImage(imageUrl:URL) -> Transcript {
var progress:Progress?
// Loop on connected peers and send the image to each
for peerID in (self.session?.connectedPeers)! {
progress = self.session?.sendResource(at: imageUrl, withName: imageUrl.lastPathComponent, toPeer: peerID, withCompletionHandler: { (error) in
// **WAS NOT CALLED!!!**
if (error != nil) {
print("Send resource to peer " + peerID.displayName + " completed with Error " + error.debugDescription)
} else {
// Create an image transcript for this received image resource
let transcript:Transcript = Transcript().initWithPeerID(peerID: (self.session?.myPeerID)!, imageUrl: imageUrl, direction: .TRANSCRIPT_DIRECTION_SEND)
self.delegate?.updateTranscript(transcript: transcript)
}
// **WAS NOT CALLED!!!**
})
}
// Create an outgoing progress transcript. For simplicity we will monitor a single NSProgress. However users can measure each NSProgress returned individually as needed
let transcript:Transcript = Transcript().initWithPeerID(peerID: (self.session?.myPeerID)!, imageName: imageUrl.lastPathComponent, progress: progress, direction: .TRANSCRIPT_DIRECTION_SEND)
return transcript
}
}