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
に設定しました。ありがとう。