I’m trying to implement a cache for storing remote videos in my SwiftUI app.
The main approach I found so far is the accepted answer in this thread: Is it possible to cache Videos? IOS – Swift
However, when I try to implement it, the video does not load:
VideoPlayer(player: player)
.onAppear {
CacheManager.shared.getFileWith(stringUrl: videoURL) { result in
switch result {
case .success(let url):
self.player = AVPlayer(url: url)
self.player?.play()
case .failure:
print("Error")
}
}
}
For context, printing the URL from CacheManager gives file:///var/mobile/Containers/Data/Application/2F12CCE9-1D0C-447B-9B96-9EC6F6BE1413/Library/Caches/filename
(where filename is the actual filename).
And this is what the video player looks like when running the app.
If I was to play directly from the videoURL
, then there would be no issues and the video plays. But I would like to cache it as well. So is there an issue with my implementation of the code? Is the accepted answer from the old thread actually invalid/outdated and no longer works with current iOS versions? Does anyone have an alternative method for caching videos?