iosobjective-cswifttvos10

Video Download in Apple tvOS


I am a newbie to swift and tvOS.I want to download a video from the url and display it in the ViewController. while i try with the ios Devices the video is downloaded and works fine.But with the tvOS the Video is not downloaded.

why is it so..?

How can i download and play the video in the Apple TV.

Here is my Code

let backgroundSessionConfiguration = URLSessionConfiguration.background(withIdentifier: "backgroundSession")

    backgroundSession = Foundation.URLSession(configuration: backgroundSessionConfiguration, delegate: self, delegateQueue: OperationQueue.main)

    let url = URL(string: "myURl")!
    downloadTask = backgroundSession.downloadTask(with: url)
    downloadTask.resume()

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL){

    let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
    let documentDirectoryPath:String = path[0]
    let fileManager = FileManager()

    let destinationURLForFile = URL(fileURLWithPath: documentDirectoryPath.appendingFormat("/file.mp4"))

    if fileManager.fileExists(atPath: destinationURLForFile.path){
        showFileWithPath(path: destinationURLForFile.path)
    }
    else{
        do {
            try fileManager.moveItem(at: location, to: destinationURLForFile)
            // show file
            showFileWithPath(path: destinationURLForFile.path)
        }catch{
            print("An error occurred while moving file to destination url")
        }
    }
}  

Solution

  • There are some restrictions in tvOS regarding Local storage

    Your app can only access 500 KB of persistent storage that is local to the device (using the NSUserDefaults class). Outside of this limited local storage, all other data must be purgeable by the operating system when space is low...

    You can take a look to this documentation for further details: https://developer.apple.com/library/content/documentation/General/Conceptual/AppleTV_PG/