Swift - Downloading video with downloadTaskWithURL

后端 未结 2 1315
无人及你
无人及你 2020-12-01 08:45

I\'m downloading a video thanks to downloadTaskWithURL and I\'m saving it to my gallery with this code :

    func saveVideoBis(fileStringURL:String){

    pr         


        
2条回答
  •  猫巷女王i
    2020-12-01 09:17

    If you want to download Video from URL but don't want to store it on Gallery. following code works for me...

     func createDownloadTask(videoURL: String,index: Int) {
        let downloadRequest = NSMutableURLRequest(url: URL(string: videoURL)!)
        let session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: OperationQueue.main)
        self.downloadTask = session.downloadTask(with: downloadRequest as URLRequest, completionHandler: { (url, response, error) in
            print("asdasd")
            if error != nil{
                if super.reachability.connection == .none{
                    self.showalert(msg: error?.localizedDescription ?? "")
                }else{
                    self.downloadTask?.resume()
                }
            }
    
            guard let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
            // create a deatination url with the server response suggested file name
            let destinationURL = documentsDirectoryURL.appendingPathComponent(response?.suggestedFilename ?? "")
    
            do {
                if url != nil{
                try FileManager.default.moveItem(at: url!, to: destinationURL)
                }
    
                }
    
            } catch { print(error) }
    
        })
        self.downloadTask!.resume()
    }
    

提交回复
热议问题