我尝试使用NSURLSession在swift中下载文件: 我的代表从未打过电话,我不知道为什么。有人可以帮忙吗?
代码: 我的班级有以下协议:
class ViewController: UIViewController, NSURLSessionDelegate {
在我的课程中,我有一个下载zip功能:
func download_zip(sURL: String, sToLocation: String) {
var delegate = self;
delegate.storePath=sToLocation;
delegate.progressView=progressView;
struct SessionProperties {
static let identifier : String! = "url_session_background_download"
}
var configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier(SessionProperties.identifier)
var backgroundSession = NSURLSession(configuration: configuration, delegate: delegate, delegateQueue: nil)
var url = NSURLRequest(URL: NSURL(string: sURL)!)
var downloadTask = backgroundSession.downloadTaskWithRequest(url)
downloadTask.resume()
}
在我的班级中我有一些代表职能:
//MARK: session delegate
func URLSession(session: NSURLSession, didBecomeInvalidWithError error: NSError?) {
println("session error: \(error?.localizedDescription).")
}
func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential!) -> Void) {
completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential, NSURLCredential(forTrust: challenge.protectionSpace.serverTrust))
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location1: NSURL) {
println("session \(session) has finished the download task \(downloadTask) of URL \(location1).")
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
println("session \(session) download task \(downloadTask) wrote an additional \(bytesWritten) bytes (total \(totalBytesWritten) bytes) out of an expected \(totalBytesExpectedToWrite) bytes.")
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
println("session \(session) download task \(downloadTask) resumed at offset \(fileOffset) bytes out of an expected \(expectedTotalBytes) bytes.")
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
if error == nil {
println("session \(session) download completed")
} else {
println("session \(session) download failed with error \(error?.localizedDescription)")
}
}
func URLSessionDidFinishEventsForBackgroundURLSession(session: NSURLSession) {
println("background session \(session) finished events.")
if !session.configuration.identifier.isEmpty {
callCompletionHandlerForSession(session.configuration.identifier)
}
}
//MARK: completion handler
func addCompletionHandler(handler: CompleteHandlerBlock, identifier: String) {
handlerQueue[identifier] = handler
}
func callCompletionHandlerForSession(identifier: String!) {
var handler : CompleteHandlerBlock = handlerQueue[identifier]!
handlerQueue!.removeValueForKey(identifier)
handler()
}
没有调用委托函数。有没有人看到这个问题?
答案 0 :(得分:1)
你有:
var backgroundSession = NSURLSession(configuration: configuration, delegate: delegate, delegateQueue: nil)
应该是:
var backgroundSession = NSURLSession(configuration: configuration, delegate: self, delegateQueue: NSOperationQueue.mainQueue())
代表必须自我设定。还要在协议中添加“NSURLSessionDataDelegate
”。
答案 1 :(得分:0)
尝试将NSURLSession存储到属性中,我认为您的会话可能会被ARC从内存中清除