【问题标题】:Error message on NSURLSessionDownloadDelegate ProtocolNSURLSessionDownloadDelegate 协议上的错误消息
【发布时间】:2015-07-17 18:00:27
【问题描述】:

我实际上正在处理下载/上传文件并跟踪进度。它运作良好。但是,我只是想知道如果出现问题怎么办,并且我没有看到此委托中的任何方法处理错误。以下是来自NSURLSessionDownloadDelegate的方法:

/*
 * Messages related to the operation of a task that writes data to a
 * file and notifies the delegate upon completion.
 */
@protocol NSURLSessionDownloadDelegate <NSURLSessionTaskDelegate>

/* Sent when a download task that has completed a download.  The delegate should 
 * copy or move the file at the given location to a new location as it will be 
 * removed when the delegate message returns. URLSession:task:didCompleteWithError: will
 * still be called.
 */
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
                              didFinishDownloadingToURL:(NSURL *)location;

@optional
/* Sent periodically to notify the delegate of download progress. */
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
                                           didWriteData:(int64_t)bytesWritten
                                      totalBytesWritten:(int64_t)totalBytesWritten
                              totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite;

/* Sent when a download has been resumed. If a download failed with an
 * error, the -userInfo dictionary of the error will contain an
 * NSURLSessionDownloadTaskResumeData key, whose value is the resume
 * data. 
 */
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask
                                      didResumeAtOffset:(int64_t)fileOffset
                                     expectedTotalBytes:(int64_t)expectedTotalBytes;

@end

只是想知道如何返回错误消息,谢谢。

【问题讨论】:

    标签: ios objective-c nsurlsessiondownloadtask


    【解决方案1】:

    让您的委托符合 NSURLSessionTaskDelegate 并实施:

    - (void)URLSession:(NSURLSession *)session
                  task:(NSURLSessionTask *)task
    didCompleteWithError:(NSError *)error
    

    编辑进一步澄清,请参阅the doc for NSURLSessionDownloadDelegate,注意靠近顶部的位置:

    除了这些方法,一定要实现 NSURLSessionTaskDelegate 和 NSURLSessionDelegate 协议来处理 所有任务类型和会话级事件共有的事件, 分别。

    这个想法是 url 会话的委托可以符合这些协议中的任何一个。您正在寻找的方法,即指示错误的方法,只是比下载任务更通用,它可以发生在任何类型的会话任务上,因此他们将其置于更抽象的协议中。

    【讨论】:

    • 我没有,因为我只看到他们的三个委托方法。能否也包括NSURLSessionDownloadDelegateNSURLSessionTaskDelegate之间的关系?
    • 是的,在声明符合NSURLSessionDownloadDelegate的地方,只要加上&lt;NSURLSessionDownloadDelegate, NSURLSessionTaskDelegate&gt;并实现error方法即可。
    • 您能否将答案包括在内,以便人们知道,因为对于像我们这样的初学者。我们不知道。这两种协议又是什么关系?
    • 请尽可能提高帖子。谢谢。
    • 对不起,我不明白你的意思
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多