我在我的项目中使用XMPPFramework而我正在尝试互相发送照片,但我被卡住了。
文件传输在第一次运行时非常好,但是当尝试发送另一个时,它失败并显示消息“正在传输”。
我搜索了很多,但是对于这种情况没有描述/建议/答案。我该怎么解决这个问题?
我的代码在这里:
按下按钮开始发送。
@IBAction func didPressedSendButton(_ sender: UIButton) {
if imageView.image == nil{
print("Select image first!")
return
}
if jidField.text == nil{
print("Set opponent jid first!")
return
}
if !XMPP.shared.stream.isConnected{
print("Stream is not connected!")
return
}
sender.isEnabled = false
let outgoingFileTransfer = XMPPOutgoingFileTransfer(dispatchQueue: .main)
outgoingFileTransfer.activate(XMPP.shared.stream)
outgoingFileTransfer.addDelegate(self, delegateQueue: .main)
let opponent = XMPPJID(string: jidField.text!)!
let recipient = XMPPJID(user: opponent.user, domain: opponent.domain, resource: "foo")
do{
outgoingFileTransfer.recipientJID = recipient
outgoingFileTransfer.outgoingData = UIImagePNGRepresentation(imageView.image!)
try outgoingFileTransfer.start()
try outgoingFileTransfer.send(
UIImagePNGRepresentation(imageView.image!),
named: imageView.image?.description ,
toRecipient: recipient,
description: "outgoingFileTransfer Description")
}catch{
print("Uh-oh... Something went wrong.")
print(error.localizedDescription)
}
}
委派方法
func xmppOutgoingFileTransfer(_ sender: XMPPOutgoingFileTransfer!, didFailWithError error: Error!) {
print("There was an error for sending a file. \(error.localizedDescription)")
sendButton.isEnabled = true
}
func xmppOutgoingFileTransferDidSucceed(_ sender: XMPPOutgoingFileTransfer!) {
print("Sending a file succeed. pray for receiving with no problem.")
sendButton.isEnabled = true
}
func xmppIncomingFileTransfer(_ sender: XMPPIncomingFileTransfer!, didFailWithError error: Error!) {
print("Incoming file transfer failed with error : ", error)
}
func xmppIncomingFileTransfer(_ sender: XMPPIncomingFileTransfer!, didReceiveSIOffer offer: XMPPIQ!) {
print("Incoming File Transfer did receive SI Offer. Accepting...")
print("Offer : ")
print(offer.prettyXMLString())
sender.acceptSIOffer(offer)
//offer.name
}
func xmppIncomingFileTransfer(_ sender: XMPPIncomingFileTransfer!, didSucceedWith data: Data!, named name: String!) {
print("I got something...")
if let receivedString = String(data: data, encoding: .utf8){
let alert = UIAlertController(title: "You\'ve got :", message: receivedString, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
print("OK, this is a text file.")
print("Incoming file transfer did Succeed. Hooray!")
}else if let receivedImage = UIImage(data: data){
receivedImageview.image = receivedImage
print("OK, this is an image file.")
print("Incoming file transfer did Succeed. Hooray!")
}else{
print("I got something, but I can\'t convert it. what is this?")
}
}
+++附加
我“成功”发送&收到第一个文件,但我不能发送第二个文件。它只返回错误消息“正在传输”,文件传输永远不会启动。
答案 0 :(得分:-1)
只需删除此代码:
try outgoingFileTransfer.start()