簡體   English   中英

當app強制退出時如何恢復下載?

[英]How to resume a download when the app force-Quit?

我的應用程序必須下載一個非常大的文件(390Mb),我正在使用TCBlopDownloadSwift進行下載(我將其轉換為swift 2.0並且工作正常)並且我進行了后台下載配置。 我想,當app強制退出時能夠恢復下載。 我發現當應用程序退出時,我仍然可以在緩存中找到下載的數據(在“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,但文件不是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, task: NSURLSessionTask, 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)
        }
    }
}

我不得不銷毀庫(如果app強制退出,它的結構不允許恢復下載)

TLDR

如果您使用具有自定義委托的庫(一個不同於NSURLSessionDelegate),問題可能來自不調用URLSession的自定義委托(會話:NSURLSession,任務:NSURLSessionTask,didCompleteWithError sessionError:NSError?)方法

PS:謝謝你理解的答案我的帖子有多誤導。

我會嘗試(如果我有時間)處理一個框架,可以允許在app force-Quit之后恢復下載(看起來很簡單,實際上你只需為該特定情況添加一個委托方法,但如果它更多復雜我沒有時間,也許以后)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM