'How do I show downloaded videos in iOS Storage settings

I've created a streaming video app that also downloads videos locally. How can I display downloaded videos in the iPhone/iPad Storage sections of settings?

I'm downloading using background tasks, and on complete running

let docsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let destinationUrl = docsUrl.appendingPathComponent(videoId + ".mp4")
let fileManager = FileManager.default
try? fileManager.removeItem(at: destinationUrl)

do {
    try fileManager.copyItem(at: location, to: destinationUrl)
} catch let error {
    print("Could not copy file to disk: \(error.localizedDescription)")
    return
}

Apps like Netflix, Disney+ and Prime Video all show the downloaded shows and allow them to be deleted individually, but I haven't been able to figure out how it's done. All searches usually lead to guides for users on how to delete videos.

Anybody have any tips?



Solution 1:[1]

This code is working for me:

func downloadVideo1(videoString :String)
{
    let myStringArr = videoString.components(separatedBy: "/")
    let finalString =  myStringArr[myStringArr.count - 1]
    let session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: OperationQueue.main)
    let docsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    let destinationUrl = docsUrl.appendingPathComponent(finalString)
    
    if(FileManager().fileExists(atPath: destinationUrl.path)){
        print("\n\nfile already exists\n\n")
    }
    else{
        // setupProgress()
        //DispatchQueue.global(qos: .background).async {
        var request = URLRequest(url: URL(string: videoString)!)
        request.httpMethod = "GET"
        _ = session.dataTask(with: request, completionHandler: { (data, response, error) in
            if(error != nil){
                print("\n\nsome error occured\n\n")
                return
            }
            if let response = response as? HTTPURLResponse{
                if response.statusCode == 200{
                    DispatchQueue.main.async {
                        if let data = data{
                            if let _ = try? data.write(to: destinationUrl, options: Data.WritingOptions.atomic){
                                print("\n\nurl data written\n\n")
                                print(destinationUrl)
                                self.checkCount = self.checkCount + 1
                                if self.checkCount == self.availableMediaCount
                                {
                                    self.progressView.isHidden = true
                                    let vc = TeacherLessonPlanSB.instantiateViewController(withIdentifier: "DownloadLessonVC") as! DownloadLessonViewController
                                    self.navigationController?.pushViewController(vc, animated: true)
                                }
                            }
                            else{
                                print("\n\nerror again\n\n")
                            }
                        }//end if let data
                    }//end dispatch main
                }//end if let response.status
            }
        }).resume()
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SwissMark