我正在尝试通过WatchConnectivity将Apple Watch的数据文件发送到配对的iPhone。我的代码如下:
let data = try? JSONSerialization.data(withJSONObject: self.storedData, options : .prettyPrinted)
let str = String(data: data!, encoding: String.Encoding.utf8)
print(str)
var fileName = ""
let tempDirectory = NSTemporaryDirectory()
fileName = tempDirectory + "test.json"
if self.fileManager.createFile(atPath: fileName, contents: data
, attributes: nil) {
print("New file creation went through!")
}
if self.fileManager.fileExists(atPath: fileName) {
print("File created @ \(fileName)")
} else {
print("Something wrong when creating file @ \(fileName)")
}
var fileTransferString = "file://"
fileTransferString += fileName
let url = URL(string: fileTransferString)
self.session.transferFile(url!, metadata: nil)
print("\(self.session.outstandingFileTransfers.count) files in the transfer queue!")
虽然我收到“File created @ ...”的消息,但最后一行“print”的以下消息始终是“传输队列中的0个文件!”我没有发现任何对象在整个过程中未能启动或无效。似乎文件已经创建但是“session.transferFile”函数没有通过。
有人可以帮我提一些关于问题所在以及如何解决问题的提示吗?
谢谢大家!
保
答案 0 :(得分:0)
尝试将最后两行更改为:
self.session.transferFile(url!, metadata: nil)
DispatchQueue.global(attributes: .qosUserInitiated).async {
print("\(self.session.outstandingFileTransfers.count) files in the transfer queue!")
}
有什么不同吗?