ios - 使用 AVAssetDownloadDelegate 检测用户取消下载的正确方法

标签 ios swift http-live-streaming avassetdownloadtask

希望有人煽动,我们尝试在苹果论坛上发帖,但没有任何帮助。尝试找到正确的方法来检测用户取消下载/使用 Swift 中的 AVAssetDownloadDelegate 从 AVAssetDownloadTask 对象中提取错误代码。我们尝试使用以下方法:

public func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL)
{
    if let errorCode = assetDownloadTask.error?._code,
        errorCode == NSURLErrorCancelled
    {
        do
        {
            try FileManager.default.removeItem(at: location)
        }
        catch
        {
            DDLogError("An error occurred trying to delete the contents on disk for \(error)")
        }

        // if we do have a cancelled download and we have removed it, return before we save the location
        return
    }

    // we need to save the location where the download finished, so when we call our delegate in
    // urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
    // we have a reference to it in order to pass along
    self.locationDict[assetDownloadTask.urlAsset.url] = location
}

以及

public func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL)  
{  
    if let error = assetDownloadTask.error as NSError?  
    {  
        switch (error.domain, error.code)  
        {  
            case (NSURLErrorDomain, NSURLErrorCancelled):  

                do  
                {  
                    try FileManager.default.removeItem(at: location)  
                }  
                catch  
                {  
                    DDLogError("An error occurred trying to delete the contents on disk for \(error)")  
                }  

                // if we do have a cancelled download and we have removed it, return before we save the location  
                return  
            default:  
                DDLogError("An unexpected error occurred \(error.domain)")  
        }  
    }  

    // we need to save the location where the download finished, so when we call our delegate in  
    // urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)  
    // we have a reference to it in order to pass along  
    self.locationDict[assetDownloadTask.urlAsset.url] = location  
}  

但是两者都显示在 Crashlytics 中并带有崩溃日志:

Crashed: com.-.ios.application.AssetDownloadUrlSession  
0  libswiftFoundation.dylib       0x103afa01c specialized static URL._unconditionallyBridgeFromObjectiveC(_:) + 8344  
1  Networking                     0x1027d2178 $S13Networking26AssetDownloadUrlSessionC03urlE0_05assetC4Task22didFinishDownloadingToySo12NSURLSessionC_So07AVAssetcH0C10Foundation3URLVtFTf4dnnn_n + 456  
2  Networking                     0x1027ce3e8 $S13Networking26AssetDownloadUrlSessionC03urlE0_05assetC4Task22didFinishDownloadingToySo12NSURLSessionC_So07AVAssetcH0C10Foundation3URLVtFTo + 88  
3  CFNetwork                      0x1dc049c78 __89-[NSURLSession delegate_AVAssetDownloadTask:didFinishDownloadingToURL:completionHandler:]_block_invoke + 36  
4  Foundation                     0x1dc33c8bc __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 16  
5  Foundation                     0x1dc244ab8 -[NSBlockOperation main] + 72  
6  Foundation                     0x1dc243f8c -[__NSOperationInternal _start:] + 740  
7  Foundation                     0x1dc33e790 __NSOQSchedule_f + 272  
8  libdispatch.dylib              0x1db2e56c8 _dispatch_call_block_and_release + 24  
9  libdispatch.dylib              0x1db2e6484 _dispatch_client_callout + 16  
10 libdispatch.dylib              0x1db28982c _dispatch_continuation_pop$VARIANT$mp + 412  
11 libdispatch.dylib              0x1db288ef4 _dispatch_async_redirect_invoke + 600  
12 libdispatch.dylib              0x1db295a18 _dispatch_root_queue_drain + 376  
13 libdispatch.dylib              0x1db2962c0 _dispatch_worker_thread2 + 128  
14 libsystem_pthread.dylib        0x1db4c917c _pthread_wqthread + 472  
15 libsystem_pthread.dylib        0x1db4cbcec start_wqthread + 4 

最佳答案

您可以实现此选项,它应该可以帮助您避免应用崩溃

 func urlSession(_ session: URLSession, assetDownloadTask: AVAssetDownloadTask, didFinishDownloadingTo location: URL) {
        guard assetDownloadTask.urlAsset.assetCache?.isPlayableOffline == true,
            assetDownloadTask.error == nil else { return }
        locationDict[assetDownloadTask.urlAsset.url] = location
    }

关于ios - 使用 AVAssetDownloadDelegate 检测用户取消下载的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55481028/

相关文章:

ios - 添加自定义数组SwiftUI的元素

ios - 在 Spritekit Xcode 中添加爆炸的最佳方法

ios - 将我的 iOS 应用程序检测为网络浏览器

ios - 如何在 firebase firestore 数据库中标记和报告用户?

ios - 具有不同长度的音频文件的 HTTP 实时流式传输

ios - AVPlayer HLS 直播流落后

ios - 当我使用 Storyboard时,无法识别 applicationWillResignActive 的功能

ios - 合并两个 sqlite3 查询

ios - 为什么 NSTimer 在 swift 中只工作一次

node.js - 有没有办法重新流式传输 HLS (m3u8) 文件,以便连接只命中我的服务器,服务器流式传输重新渲染的文件,而不是原始流?