Skip to content

Instantly share code, notes, and snippets.

@psobko
Created February 25, 2017 20:00
Show Gist options
  • Save psobko/9999c14a7a366d43bb50fd369ee616e6 to your computer and use it in GitHub Desktop.
Save psobko/9999c14a7a366d43bb50fd369ee616e6 to your computer and use it in GitHub Desktop.
Log all of the delegate callbacks for URLSession in Swift 3.0
// use URLSessionDelegate
var session : URLSession!
func someFunction() {
self.session = URLSession(configuration: URLSessionConfiguration.default, delegate: self, delegateQueue: nil)
}
func dLog(message: String, filename: String = #file, function: String = #function, line: Int = #line) {
print("[\((filename as NSString).lastPathComponent):\(line)] \(function) - \(message)")
}
/*
* URLSessionDelegate : NSObjectProtocol
*/
func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?)
{
dLog(message:"")
}
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Swift.Void)
{
dLog(message:"")
}
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession)
{
dLog(message:"")
}
/*
* URLSessionTaskDelegate : URLSessionDelegate
*/
func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Swift.Void)
{
dLog(message:"")
}
func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Swift.Void)
{
dLog(message:"")
}
func urlSession(_ session: URLSession, task: URLSessionTask, needNewBodyStream completionHandler: @escaping (InputStream?) -> Swift.Void)
{
dLog(message:"")
}
func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64)
{
dLog(message:"")
}
func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics)
{
dLog(message:"")
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
{
dLog(message:"")
}
/*
* URLSessionDataDelegate : URLSessionTaskDelegate
*/
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Swift.Void)
{
dLog(message:"")
}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didBecome downloadTask: URLSessionDownloadTask)
{
dLog(message:"")
}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didBecome streamTask: URLSessionStreamTask)
{
dLog(message:"")
}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data)
{
dLog(message:"")
}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, willCacheResponse proposedResponse: CachedURLResponse, completionHandler: @escaping (CachedURLResponse?) -> Swift.Void)
{
dLog(message:"")
}
/*
* URLSessionDownloadDelegate : URLSessionTaskDelegate
*/
/* 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.
*/
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL)
{
dLog(message:"")
}
/* Sent periodically to notify the delegate of download progress. */
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64)
{
dLog(message:"")
}
/* 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.
*/
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64)
{
dLog(message:"")
}
/*
* URLSessionStreamDelegate : URLSessionTaskDelegate
*/
/* Indiciates that the read side of a connection has been closed. Any
* outstanding reads complete, but future reads will immediately fail.
* This may be sent even when no reads are in progress. However, when
* this delegate message is received, there may still be bytes
* available. You only know that no more bytes are available when you
* are able to read until EOF. */
func urlSession(_ session: URLSession, readClosedFor streamTask: URLSessionStreamTask)
{
dLog(message:"")
}
/* Indiciates that the write side of a connection has been closed.
* Any outstanding writes complete, but future writes will immediately
* fail.
*/
func urlSession(_ session: URLSession, writeClosedFor streamTask: URLSessionStreamTask)
{
dLog(message:"")
}
/* A notification that the system has determined that a better route
* to the host has been detected (eg, a wi-fi interface becoming
* available.) This is a hint to the delegate that it may be
* desirable to create a new task for subsequent work. Note that
* there is no guarantee that the future task will be able to connect
* to the host, so callers should should be prepared for failure of
* reads and writes over any new interface. */
func urlSession(_ session: URLSession, betterRouteDiscoveredFor streamTask: URLSessionStreamTask)
{
dLog(message:"")
}
/* The given task has been completed, and unopened NSInputStream and
* NSOutputStream objects are created from the underlying network
* connection. This will only be invoked after all enqueued IO has
* completed (including any necessary handshakes.) The streamTask
* will not receive any further delegate messages.
*/
func urlSession(_ session: URLSession, streamTask: URLSessionStreamTask, didBecome inputStream: InputStream, outputStream: OutputStream)
{
dLog(message:"")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment