diff --git a/Sources/NetShears/NetShears.swift b/Sources/NetShears/NetShears.swift index 8c28cfe..7620d1a 100644 --- a/Sources/NetShears/NetShears.swift +++ b/Sources/NetShears/NetShears.swift @@ -18,10 +18,16 @@ public extension BodyExporterDelegate { func netShears(exportRequestBodyFor request: NetShearsRequestModel) -> BodyExportType { .default } } +public protocol TaskProgressDelegate: AnyObject { + func task(_ url: URL, didRecieveProgress progress: Progress) +} + public final class NetShears: NSObject { public static let shared = NetShears() public weak var bodyExportDelegate: BodyExporterDelegate? + public weak var taskProgressDelegate: TaskProgressDelegate? + internal var loggerEnable = false internal var interceptorEnable = false internal var listenerEnable = false diff --git a/Sources/NetShears/URLProtocol/NetworkInterceptorUrlProtocol.swift b/Sources/NetShears/URLProtocol/NetworkInterceptorUrlProtocol.swift index eea4813..c8db884 100644 --- a/Sources/NetShears/URLProtocol/NetworkInterceptorUrlProtocol.swift +++ b/Sources/NetShears/URLProtocol/NetworkInterceptorUrlProtocol.swift @@ -121,5 +121,11 @@ extension NetworkInterceptorUrlProtocol: URLSessionDataDelegate { func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) { client?.urlProtocolDidFinishLoading(self) } + + func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) { + if let url = task.currentRequest?.url { + NetShears.shared.taskProgressDelegate?.task(url, didRecieveProgress: task.progress) + } + } } diff --git a/Sources/NetShears/URLProtocol/NetworkLoggerUrlProtocol.swift b/Sources/NetShears/URLProtocol/NetworkLoggerUrlProtocol.swift index 4cbe81d..bcdc93e 100644 --- a/Sources/NetShears/URLProtocol/NetworkLoggerUrlProtocol.swift +++ b/Sources/NetShears/URLProtocol/NetworkLoggerUrlProtocol.swift @@ -137,6 +137,12 @@ extension NetworkLoggerUrlProtocol: URLSessionDataDelegate { func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) { client?.urlProtocolDidFinishLoading(self) } + + func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) { + if let url = task.currentRequest?.url { + NetShears.shared.taskProgressDelegate?.task(url, didRecieveProgress: task.progress) + } + } } diff --git a/Sources/NetShears/URLProtocol/NetwrokListenerUrlProtocol.swift b/Sources/NetShears/URLProtocol/NetwrokListenerUrlProtocol.swift index cb86972..edb4441 100644 --- a/Sources/NetShears/URLProtocol/NetwrokListenerUrlProtocol.swift +++ b/Sources/NetShears/URLProtocol/NetwrokListenerUrlProtocol.swift @@ -137,6 +137,12 @@ extension NetwrokListenerUrlProtocol: URLSessionDataDelegate { func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) { client?.urlProtocolDidFinishLoading(self) } + + func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) { + if let url = task.currentRequest?.url { + NetShears.shared.taskProgressDelegate?.task(url, didRecieveProgress: task.progress) + } + } }