iOS NSURLSession Listen to Timeout

828 views Asked by At

I created a simple NSURLSessionDownloadTask to download from a URL, with its class having the NSURLSession delegates:

@interface DownloadManager : NSObject <NSURLSessionDataDelegate, NSURLSessionDelegate, NSURLSessionDownloadDelegate, NSURLSessionTaskDelegate>

//...

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
[sessionConfiguration setTimeoutIntervalForRequest:30.0];
[sessionConfiguration setTimeoutIntervalForResource:60.0];

NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:self.url];

[downloadTask resume];

However, I could not find a protocol method that listens to the download task timing out. Is there a way to listen to the timeout (ex. - I wanted to close a progress dialog box when 30.0 seconds have passed and no data is still received)

I've already scavenged Google but haven't found any information so far, so I'll leave this question here while I search for more info.

Thanks so much!

1

There are 1 answers

0
dopcn On

The timeout is one of the errors NSURLSession will give you in completionHandler block. It's NSURLErrorTimedOut = -1001.

in delegate method

- URLSession:task:didCompleteWithError:

check the NSError if it's NSURLErrorTimedOut do what you want

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/#//apple_ref/doc/constant_group/URL_Loading_System_Error_Codes