-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathURLSessionDelegateProxy.swift
More file actions
204 lines (164 loc) · 9.08 KB
/
URLSessionDelegateProxy.swift
File metadata and controls
204 lines (164 loc) · 9.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//
// URLSessionDelegateProxy.swift
// JavaScriptCoreBrowserObjectModel
//
// Created by Connor Grady on 1/20/18.
// Copyright © 2018 Connor Grady. All rights reserved.
//
import Foundation
class URLSessionDelegateProxy: NSObject {
weak var target: URLSessionDelegate?
init(target: URLSessionDelegate? = nil) {
super.init()
self.target = target
}
override func responds(to aSelector: Selector!) -> Bool {
return target?.responds(to: aSelector) ?? super.responds(to: aSelector)
}
override func forwardingTarget(for aSelector: Selector!) -> Any? {
return target
}
}
extension URLSessionDelegateProxy: URLSessionDelegate {
/*
@available(iOS 7.0, *)
func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) {
target?.urlSession?(session, didBecomeInvalidWithError: error)
}
@available(iOS 7.0, *)
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Swift.Void) {
guard target != nil, target!.responds(to: #function) else {
completionHandler(.performDefaultHandling, nil)
return
}
target?.urlSession?(session, didReceive: challenge, completionHandler: completionHandler)
}
@available(iOS 7.0, *)
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
target?.urlSessionDidFinishEvents?(forBackgroundURLSession: session)
}
*/
}
extension URLSessionDelegateProxy: URLSessionTaskDelegate {
/*
@available(iOS 11.0, *)
func urlSession(_ session: URLSession, task: URLSessionTask, willBeginDelayedRequest request: URLRequest, completionHandler: @escaping (URLSession.DelayedRequestDisposition, URLRequest?) -> Swift.Void) {
guard target != nil, target!.responds(to: #function) else {
completionHandler(URLSession.DelayedRequestDisposition.continueLoading, nil)
return
}
(target as? URLSessionTaskDelegate)?.urlSession?(session, task: task, willBeginDelayedRequest: request, completionHandler: completionHandler)
}
@available(iOS 11.0, *)
func urlSession(_ session: URLSession, taskIsWaitingForConnectivity task: URLSessionTask) {
(target as? URLSessionTaskDelegate)?.urlSession?(session, taskIsWaitingForConnectivity: task)
}
@available(iOS 7.0, *)
func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, completionHandler: @escaping (URLRequest?) -> Swift.Void) {
guard target != nil, target!.responds(to: #function) else {
completionHandler(request)
//completionHandler(nil) // refuse the redirect and return the body of the redirect response
return
}
(target as? URLSessionTaskDelegate)?.urlSession?(session, task: task, willPerformHTTPRedirection: response, newRequest: request, completionHandler: completionHandler)
}
@available(iOS 7.0, *)
func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Swift.Void) {
guard target != nil, target!.responds(to: #function) else {
completionHandler(.performDefaultHandling, nil)
return
}
(target as? URLSessionTaskDelegate)?.urlSession?(session, didReceive: challenge, completionHandler: completionHandler)
}
@available(iOS 7.0, *)
func urlSession(_ session: URLSession, task: URLSessionTask, needNewBodyStream completionHandler: @escaping (InputStream?) -> Swift.Void) {
guard target != nil, target!.responds(to: #function) else {
completionHandler(nil)
return
}
(target as? URLSessionTaskDelegate)?.urlSession?(session, task: task, needNewBodyStream: completionHandler)
}
@available(iOS 7.0, *)
func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
(target as? URLSessionTaskDelegate)?.urlSession?(session, task: task, didSendBodyData: bytesSent, totalBytesSent: totalBytesSent, totalBytesExpectedToSend: totalBytesExpectedToSend)
}
@available(iOS 10.0, *)
func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
(target as? URLSessionTaskDelegate)?.urlSession?(session, task: task, didFinishCollecting: metrics)
}
@available(iOS 7.0, *)
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
(target as? URLSessionTaskDelegate)?.urlSession?(session, task: task, didCompleteWithError: error)
}
*/
}
extension URLSessionDelegateProxy: URLSessionDataDelegate {
/*
@available(iOS 7.0, *)
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Swift.Void) {
guard target != nil, target!.responds(to: #function) else {
completionHandler(.allow)
return
}
(target as? URLSessionDataDelegate)?.urlSession?(session, dataTask: dataTask, didReceive: response, completionHandler: completionHandler)
}
@available(iOS 7.0, *)
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didBecome downloadTask: URLSessionDownloadTask) {
(target as? URLSessionDataDelegate)?.urlSession?(session, dataTask: dataTask, didBecome: downloadTask)
}
@available(iOS 9.0, *)
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didBecome streamTask: URLSessionStreamTask) {
(target as? URLSessionDataDelegate)?.urlSession?(session, dataTask: dataTask, didBecome: streamTask)
}
@available(iOS 7.0, *)
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
(target as? URLSessionDataDelegate)?.urlSession?(session, dataTask: dataTask, didReceive: data)
}
@available(iOS 7.0, *)
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, willCacheResponse proposedResponse: CachedURLResponse, completionHandler: @escaping (CachedURLResponse?) -> Swift.Void) {
guard target != nil, target!.responds(to: #function) else {
completionHandler(proposedResponse)
//completionHandler(nil) // prevent caching
return
}
(target as? URLSessionDataDelegate)?.urlSession?(session, dataTask: dataTask, willCacheResponse: proposedResponse, completionHandler: completionHandler)
}
*/
}
extension URLSessionDelegateProxy: URLSessionDownloadDelegate {
// REQUIRED
@available(iOS 7.0, *)
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
(target as? URLSessionDownloadDelegate)?.urlSession(session, downloadTask: downloadTask, didFinishDownloadingTo: location)
}
/*
@available(iOS 7.0, *)
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
(target as? URLSessionDownloadDelegate)?.urlSession?(session, downloadTask: downloadTask, didWriteData: bytesWritten, totalBytesWritten: totalBytesWritten, totalBytesExpectedToWrite: totalBytesExpectedToWrite)
}
@available(iOS 7.0, *)
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
(target as? URLSessionDownloadDelegate)?.urlSession?(session, downloadTask: downloadTask, didResumeAtOffset: fileOffset, expectedTotalBytes: expectedTotalBytes)
}
*/
}
extension URLSessionDelegateProxy: URLSessionStreamDelegate {
/*
@available(iOS 9.0, *)
func urlSession(_ session: URLSession, readClosedFor streamTask: URLSessionStreamTask) {
(target as? URLSessionStreamDelegate)?.urlSession?(session, readClosedFor: streamTask)
}
@available(iOS 9.0, *)
func urlSession(_ session: URLSession, writeClosedFor streamTask: URLSessionStreamTask) {
(target as? URLSessionStreamDelegate)?.urlSession?(session, writeClosedFor: streamTask)
}
@available(iOS 9.0, *)
func urlSession(_ session: URLSession, betterRouteDiscoveredFor streamTask: URLSessionStreamTask) {
(target as? URLSessionStreamDelegate)?.urlSession?(session, betterRouteDiscoveredFor: streamTask)
}
@available(iOS 9.0, *)
func urlSession(_ session: URLSession, streamTask: URLSessionStreamTask, didBecome inputStream: InputStream, outputStream: OutputStream) {
(target as? URLSessionStreamDelegate)?.urlSession?(session, streamTask: streamTask, didBecome: inputStream, outputStream: outputStream)
}
*/
}