Is it possible to cache Videos? IOS - Swift
Using Haneke, I wasn't able to retrieve file path for cached video. I handled it by saving video in cached directory.
public enum Result<T> {
case success(T)
case failure(NSError)
}
class CacheManager {
static let shared = CacheManager()
private let fileManager = FileManager.default
private lazy var mainDirectoryUrl: URL = {
let documentsUrl = self.fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first!
return documentsUrl
}()
func getFileWith(stringUrl: String, completionHandler: @escaping (Result<URL>) -> Void ) {
let file = directoryFor(stringUrl: stringUrl)
//return file path if already exists in cache directory
guard !fileManager.fileExists(atPath: file.path) else {
completionHandler(Result.success(file))
return
}
DispatchQueue.global().async {
if let videoData = NSData(contentsOf: URL(string: stringUrl)!) {
videoData.write(to: file, atomically: true)
DispatchQueue.main.async {
completionHandler(Result.success(file))
}
} else {
DispatchQueue.main.async {
completionHandler(Result.failure(NSError.errorWith(text: "Can't download video")))
}
}
}
}
private func directoryFor(stringUrl: String) -> URL {
let fileURL = URL(string: stringUrl)!.lastPathComponent
let file = self.mainDirectoryUrl.appendingPathComponent(fileURL)
return file
}
}
Sample usage of this class looks like this:
CacheManager.shared.getFileWith(stringUrl: "http://techslides.com/demos/sample-videos/small.mp4") { result in
switch result {
case .success(let url):
// do some magic with path to saved video
case .failure(let error):
// handle errror
}
}
Access files in /var/mobile/Containers/Data/Application without jailbreaking iPhone
If this is your app, if you connect the device to your computer, you can use the "Devices" option on Xcode's "Window" menu and then download the app's data container to your computer. Just select your app from the list of installed apps, and click on the "gear" icon and choose "Download Container".
Once you've downloaded it, right click on the file in the Finder and choose "Show Package Contents".
Related Topics
Programmatically Set Uipageviewcontroller Transition Style to Scroll
How Is This Slide-Up Menu from the iPhone Messages App Implemented
Wake Up Application in Background Using Audiosession Like Alarmy iOS App
When Should I Use Anyobject Insted of Uibutton in Swift
Swiftui How Add Custom Modifier with Callback
Validate Jwt Token with Rs256 or Rs512 with Swift iOS
Present Specific View Controller in Didreceiveremotenotification with Swift
Does Not Have a Member 'Instantiatewithowner'
Most Efficient/Realtime Way to Get Pixel Values from iOS Camera Feed in Swift
Daily Local Notifications Are Not Working
Could Not Build Module 'Nanopb' Error in Xcode 12.0.1
How to Use Type Erasure with a Protocol Using Associated Type
Peek/Pop Preview Ignores Cell Corner Radius in Collection View
How to Handle Local Notification When App Has Been Terminated
Hidden Property Cannot Be Changed Within an Animation Block
Swift 1.2 and Parse: Issue with Retrieving Images to Populate Pfquerycollectionviewcontroller