Swift Language
디스크 공간에 캐싱
수색…
소개
URLSession
및 FileManager
사용하여 비디오, 이미지 및 오디오 캐싱
절약
let url = "https://path-to-media"
let request = URLRequest(url: url)
let downloadTask = URLSession.shared.downloadTask(with: request) { (location, response, error) in
guard let location = location,
let response = response,
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first else {
return
}
let documentsDirectoryUrl = URL(fileURLWithPath: documentsPath)
let documentUrl = documentsDirectoryUrl.appendingPathComponent(response.suggestedFilename)
let _ = try? FileManager.default.moveItem(at: location, to: documentUrl)
// documentUrl is the local URL which we just downloaded and saved to the FileManager
}.resume()
독서
let url = "https://path-to-media"
guard let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first,
let searchQuery = url.absoluteString.components(separatedBy: "/").last else {
return nil
}
do {
let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil, options: [])
let cachedFiles = directoryContents.filter { $0.absoluteString.contains(searchQuery) }
// do something with the files found by the url
} catch {
// Could not find any files
}
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow