WCSession didReceive 文件不可移动 "No such file or directory"

WCSession didReceive file not movable "No such file or directory"

将文件从 iPhone 传输到 Apple Watch 后出现错误

Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"

我做错了什么?这些是代码片段:

iPhone ViewController

func makeAction () {
    let url = NSURL.fileURL(withPath: fileArray[0].object(at: 2) as! String)
    var applicationDict = Dictionary<String, Array<AnyObject>>()
    applicationDict["fileArray"] = fileArray
    WCSession.default().transferFile(url, metadata: applicationDict)
}

观看接口控制器

func session(_ session: WCSession, didReceive file: WCSessionFile) {
DispatchQueue.main.async(execute: { () -> Void in
    print("RECEIVED")
    var applicationDict = Dictionary<String, Array<AnyObject>>()
    applicationDict = file.metadata as! Dictionary<String, Array<AnyObject>>
    self.fileArray = applicationDict["fileArray"]!
    self.fileList = self.fileArray

    let dirPaths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let tempDocsDir = dirPaths[0] as String
    let docsDir = tempDocsDir.appending("/")
    let filemgr = FileManager.default

    do {
        let fileName = self.fileArray[0].object(at: 1) as! String
        try filemgr.moveItem(atPath: file.fileURL.path, toPath: docsDir + fileName)
    } catch let error as NSError {
        print("Error moving file: \(error.description)")
    }
    self.loadTableData()
})
}

完整错误消息

Error moving file: Error Domain=NSCocoaErrorDomain Code=4 "“5d1392cd-acac-4b99-abf5-50062e12dc14_95de54df-69b1-43df-bb90-cfac6fed3677.mp3” couldn’t be moved to “Documents” because either the former doesn't exist, or the folder containing the latter doesn't exist." UserInfo={NSSourceFilePathErrorKey=/Users/pknapp/Library/Developer/CoreSimulator/Devices/950FC0DA-C245-4326-8777-80CE765AF655/data/Containers/Data/PluginKitPlugin/73C0D94F-483C-4426-B052-001E8837D83A/Documents/Inbox/com.apple.watchconnectivity/FCE7E6CB-2452-4E0A-9AFF-F5B3A51A0DE8/Files/0B96CCB0-A2E1-418B-9859-97C22238A5F5/5d1392cd-acac-4b99-abf5-50062e12dc14_95de54df-69b1-43df-bb90-cfac6fed3677.mp3, NSUserStringVariant=( Move ), NSFilePath=/Users/pknapp/Library/Developer/CoreSimulator/Devices/950FC0DA-C245-4326-8777-80CE765AF655/data/Containers/Data/PluginKitPlugin/73C0D94F-483C-4426-B052-001E8837D83A/Documents/Inbox/com.apple.watchconnectivity/FCE7E6CB-2452-4E0A-9AFF-F5B3A51A0DE8/Files/0B96CCB0-A2E1-418B-9859-97C22238A5F5/5d1392cd-acac-4b99-abf5-50062e12dc14_95de54df-69b1-43df-bb90-cfac6fed3677.mp3, NSDestinationFilePath=/Users/pknapp/Library/Developer/CoreSimulator/Devices/950FC0DA-C245-4326-8777-80CE765AF655/data/Containers/Data/PluginKitPlugin/73C0D94F-483C-4426-B052-001E8837D83A/Documents/5d1392cd-acac-4b99-abf5-50062e12dc14_95de54df-69b1-43df-bb90-cfac6fed3677.mp3, NSUnderlyingError=0x7b776110 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

好的,知道了。将它放在异步调度中是错误的。没有 ist -> 完美工作。请继续,这里没什么可看的:)

didReceiveFile 注释的文档:

File: The object containing the URL of the file and any additional information. If you want to keep the file referenced by this parameter, you must move it synchronously to a new location during your implementation of this method. If you do not move the file, the system deletes it after this method returns.

因此,在将文件移动到您的应用程序有权访问的位置之前,请确保不要在此方法中异步。