使用 NSURLSessionDownloadTask 下载文件
Downloading File with NSURLSessionDownloadTask
我需要使用我的桌面应用程序下载一个文件。首先,我创建了一个简单的 iOS 项目来下载视频文件,参考这个 web site。有用。我想为 macOS 做几乎相同的事情。
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")
}
}
}
如果我 运行 它,我会收到一条日志消息,上面写着 "unknown error." 我想知道我做错了什么?我确实将 App Transport Security Setting
> Allow Allow Arbitrary Loads
设置为 YES
。谢谢。
由于您的应用是沙盒化的,因此请确保在沙盒功能中启用传出连接。
ATS 设置对 HTTPS 连接没有影响
我需要使用我的桌面应用程序下载一个文件。首先,我创建了一个简单的 iOS 项目来下载视频文件,参考这个 web site。有用。我想为 macOS 做几乎相同的事情。
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")
}
}
}
如果我 运行 它,我会收到一条日志消息,上面写着 "unknown error." 我想知道我做错了什么?我确实将 App Transport Security Setting
> Allow Allow Arbitrary Loads
设置为 YES
。谢谢。
由于您的应用是沙盒化的,因此请确保在沙盒功能中启用传出连接。
ATS 设置对 HTTPS 连接没有影响