Download in documents directory or move file async - iOS

后端 未结 2 1913
醉酒成梦
醉酒成梦 2021-02-07 12:32

I am building an iOS app in which the user can download different files.
I am using an URLSessionDownloadTask and an URLSession to download a file

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-07 12:46

    I came across the same issue but i solve it.

    First check that the file is exist in that path because i got issue because of the path extension are different of location URL. i was trying to rename audio but path extension was different(eg. mp3 to m4a)

    Also in case there is any other file already exists at destination path this issue arise.

    So first try to check file exists at location where you by using

     let fileManager = FileManager.default
    
     if fileManager.fileExists(atPath: location.path) {
         do {
             try fileManager.moveItem(at: location, to: dest)
             return true
         } catch {
             print("\(error.localizedDescription)")
             return false
         }
     }
    

    Hope this will help you

提交回复
热议问题