2015-05-29 61 views
0

我嘗試使用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() 
} 

不調用委託函數。有沒有人看到這個問題?

+0

是否NSURLSessionDownloadDelegate子類NSURLSessionDelegate? – Leonardo

+0

您是否將NSURLSessionDelegate和NSURLSessionDownloadDelegate的委託設置爲您的類? – Icaro

+0

感謝您的幫助。對困惑感到抱歉。 protocoll NSURLSessionDownloadDelegate是我的一個嘗試。我會更新這個問題並刪除它。我想沒有必要使用這個協議。 –

回答

0

嘗試存儲NSURLSession到屬性,我認爲您的會話可能會被ARC從內存中清除

+0

感謝您的幫助,但它仍然無法正常工作。 –

0

您有:

var backgroundSession = NSURLSession(configuration: configuration, delegate: delegate, delegateQueue: nil) 

它應該是:

var backgroundSession = NSURLSession(configuration: configuration, delegate: self, delegateQueue: NSOperationQueue.mainQueue()) 

代表必須設置爲自我。還要將「NSURLSessionDataDelegate」添加到協議中。

+0

swift 4重命名爲OperationQueue.main –