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

a5g8bdjr  于 2023-01-27  发布在  iOS
关注(0)|答案(3)|浏览(150)

希望有人能有所启发,我们尝试在苹果论坛发帖,但没有任何帮助。尝试找到正确的方法来检测用户取消下载/使用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
hvvq6cgz

hvvq6cgz1#

你可以实现这个选项,它应该可以帮助你避免应用程序崩溃

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
    }
pftdvrlh

pftdvrlh2#

为什么你要尝试删除文件,如果它加载失败?
自我位置字典[资产下载任务URL资产URL] =位置
我认为崩溃在这里[assetDownloadTask. urlAsset.url]在这种情况下,该值可能为零

zyfwsgd6

zyfwsgd63#

面对同样的问题,AVURLAsset的url属性可以是nil,即使它没有被定义为可选的。下面是一个丑陋但失败的可靠解决方案:

private extension AVURLAsset {
    var isURLNil: Bool {
        debugDescription.contains("URL = (null)")
    }
}

显然,这可能会打断(不太可能)Swift打印调试描述的未来变化,但在此之前,他们可能也会在框架中修复这个bug。

相关问题