我尝试使用NSURLSession在swift中下载文件: 我的代理从未被调用过,我不知道为什么。有人可以帮忙吗?使用NSURLSession下载代理(IOS,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()
}
不调用委托函数。有没有人看到这个问题?
是否NSURLSessionDownloadDelegate子类NSURLSessionDelegate? – Leonardo
您是否将NSURLSessionDelegate和NSURLSessionDownloadDelegate的委托设置为您的类? – Icaro
感谢您的帮助。对困惑感到抱歉。 protocoll NSURLSessionDownloadDelegate是我的一个尝试。我会更新这个问题并删除它。我想没有必要使用这个协议。 –