0
데스크톱 응용 프로그램과 함께 파일을 다운로드해야합니다. 먼저, 비디오 파일을 다운로드하는 간단한 iOS 프로젝트를 만들었습니다.이 프로젝트는 web site입니다. 그것은 작동합니다. 그리고 저는 macOS에 대해서도 똑같이하고 싶습니다.NSURLSessionDownloadTask를 사용하여 파일 다운로드
class ViewController: NSViewController, URLSessionDownloadDelegate {
var downloadTask: URLSessionDownloadTask!
var backgroundSession: URLSession!
var assetFile = String() // file in application's sandboxed folder
override func viewDidLoad() {
super.viewDidLoad()
let backgroundSessionConfiguration = URLSessionConfiguration.background(withIdentifier: "backgroundSession")
backgroundSession = Foundation.URLSession(configuration: backgroundSessionConfiguration, delegate: self, delegateQueue: OperationQueue.main)
}
override func viewDidAppear() {
super.viewDidAppear()
let url = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")!
downloadTask = backgroundSession.downloadTask(with: url)
downloadTask.resume()
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
let fileManager = FileManager()
do {
let assetURL = URL(fileURLWithPath: assetFile)
try fileManager.moveItem(at: location, to: assetURL)
} catch {
print("An error occurred while moving file to destination url")
}
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
downloadTask = nil
if (error != nil) {
print(error!.localizedDescription) // <<< error here?
} else {
print("The task finished transferring data successfully")
}
}
}
실행하면 "알 수없는 오류"라는 메시지가 나타납니다. 내가 뭘 잘못하고 있는지 궁금해? 나는 App Transport Security Setting
>Allow Allow Arbitrary Loads
을 YES
으로 설정했습니다. 감사.