我正在使用TCBlopDownload Swift进行下载(我将其转换为swift 2.0,它的工作正常),我做了一个后台下载的配置.
我想,当应用程序强制退出以恢复下载.
我发现当应用程序退出时,我仍然可以在缓存中找到下载的数据(在“com.apple.nsurlsessiond / Downloads /”bundleIdentifier中)作为tmp文件.
但是当我尝试获取下载的数据时:
func dataInCacheForName (name : String) -> NSData? {
    let PathToCache = (NSSearchPathForDirectoriesInDomains( NSSearchPathDirectory.CachesDirectory,.UserDomainMask,true)[0] as Nsstring).stringByAppendingPathComponent("com.apple.nsurlsessiond/Downloads/" + bundleIdentifier)
    let path = (PathToCache as Nsstring).stringByAppendingPathComponent(name)
    let data = NSData(contentsOfFile: path)
    print("data : \(data?.length)")
    return data
} 
 它返回nil,但文件不为零.我可以移动文件,所以我试图移动文件中的文件.但是如果我尝试使用数据恢复下载,我会收到错误:
-[NSKeyedUnarchiver initForReadingWithData:]: data is NULL -[NSKeyedUnarchiver initForReadingWithData:]: data is NULL -[NSKeyedUnarchiver initForReadingWithData:]: data is NULL
后台下载的简历数据无效.后台下载必须使用http或https,并且必须下载到可访问的文件.
并在URLSession中
(session: NSURLSession,task: NSURLSessionTask,didCompleteWithError sessionError: NSError?) error userInfo : Optional([:]) : Optional(Error Domain=NSURLErrorDomain Code=-3003 "(null)")
错误代码-3003表示无法写入文件
我读过很多帖子,但找不到答案
最有希望的是https://forums.developer.apple.com/thread/24770
解决方法
TCBlobDownloadSwift有一个自定义委托,它在urlSessionDelegate方法的末尾被调用(例如,自定义委托给出进度值而不是totalByteWritten和totalBytesExpectedToWrite). :
func URLSession(session: NSURLSession,downloadTask: NSURLSessionDownloadTask,didWriteData bytesWritten: Int64,totalBytesWritten: Int64,totalBytesExpectedToWrite: Int64) {
    guard let download = self.downloads[downloadTask.taskIdentifier] else{
        return
    }
    let progress = totalBytesExpectedToWrite == NSURLSessionTransferSizeUnkNown ? -1 : Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
    print("progress : \(progress)")
    // the delegate is in fact called download and has more parameter .
    customDelegate(download,progress : progress)
} 
 它工作正常.但是当应用程序重新启动时,如果恢复下载,则没有注册下载,并且downloadTask.taskIdentifier返回nil,因此自定义委托不被调用!
为了在强制退出后恢复下载,您必须使用此代码(当NSURLSessionDelegate协议中的对象已创建时调用此方法):
public func URLSession(session: NSURLSession,didCompleteWithError sessionError: NSError?) {
    if let error = sessionError {
    print("error : \(error)")
    let directory = NSURL(fileURLWithPath: fileManage.Path)
    if let resumedData = error.userInfo[NSURLSessionDownloadTaskResumeData] as? NSData {
        let url = error.userInfo[NSURLErrorFailingURLStringErrorKey] as? String
        // get name from url just read a dictionnary of name and url
        let name = getNameFromURL(url!) 
        // start the download
        self.manager.downloadFileWithResumeData(resumedData,toDirectory: directory,withName: name,andDelegate: self)
        }
    }
} 
 我不得不摧毁图书馆(它的结构不允许恢复下载如果应用程序强制退出)
TLDR
如果您使用带有自定义委托(与NSURLSessionDelegate不同)的库,问题可能来自不调用URLSession的自定义委托(会话:NSURLSession,任务:NSURLSessionTask,didCompleteWithError sessionError:NSError?)方法
PS:谢谢你的回答我明白我的帖子有多么误导性.
我会尝试(如果我有时间)工作的框架,可以允许恢复下载后,应用程序强制退出(它看起来很简单,实际上你只需要添加一个委托方法,但如果它更多复杂的我还没有时间可能以后)