URLSessionDelegate methods not called
You must conform to the relevant protocols, e.g.:
extension MyNetworkManager: URLSessionDelegate {
// this is intentionally blank
// obviously, if you implement any delegate methods for this protocol, put them here
}
extension MyNetworkManager: URLSessionDownloadDelegate {
func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didWriteData bytesWritten: Int64,
totalBytesWritten: Int64,
totalBytesExpectedToWrite: Int64) {
print(#function)
}
func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didFinishDownloadingTo location: URL) {
print(#function)
}
}
extension MyNetworkManager: URLSessionTaskDelegate {
func urlSession(_ session: URLSession,
task: URLSessionTask,
didCompleteWithError error: Error?) {
print(#function, error ?? "No error")
}
}
If you don’t conform to URLSessionDownloadDelegate
, it won’t call URLSessionDownloadDelegate
methods.
URLSession delegate methods don't works
according the comment, i edited code and it's worked.
class ViewController: UIViewController, URLSessionTaskDelegate, URLSessionDelegate, URLSessionDataDelegate {
var httpString = "hided"
override func viewDidLoad() {
super.viewDidLoad()
getLogBinData()
}
func getLogBinData() {
let session = URLSession(configuration: .default, delegate: self, delegateQueue: OperationQueue.main)
if let url = URL(string: httpString + "log.bin") {
var request = URLRequest(url: url)
request.httpMethod = "GET"
let task = session.dataTask(with: request)
task.resume()
}
}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
if dataTask.countOfBytesReceived >= 500 {
print(dataTask.countOfBytesReceived)
}
}
}
`URLSessionDelegate` methods are not called
The func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL)
method you're implementing is part of URLSessionDownloadDelegate
, not URLSessionDelegate
, so you should declare conformance to URLSessionDownloadDelegate
as well in order for the delegate call to occur.
URLSessionDataDelegate methods not being called
You only declare conformance to URLSessionDelegate
in your extension. If you declare conformance to URLSessionDataDelegate
instead, your delegate methods will be called.
URLSessionDelegate class deinit not called
URLSession
keeps a strong reference to its delegate (self
in your case). Check the official documentation:
The session object keeps a strong reference to the delegate until your app exits or explicitly invalidates the session. If you don’t invalidate the session, your app leaks memory until the app terminates.
You can invalidate the session using its methods finishTasksAndInvalidate()
or invalidateAndCancel()
. After that URLSession will release the strong reference to its delegate.
I guess you code example is just to demonstrate the behavior, but anyway I have to mention it's not a good practice to create a new URLSession for every single URLRequest.
URLSessionDataDelegate not being called using http2 connection
It's because the first 512 bytes are buffered: https://forums.developer.apple.com/thread/64875
Related Topics
How to Load Multiple Storyboard Files Depending on iOS Version? (5 and 6)
Swift- Custom Uitableviewcell Delegate to Uiviewcontroller Only One Protocol Works
Getting System Uptime in iOS/Swift
Open Viewcontroller from Tab Like in Instagram Camera Page on iOS
How to Record a Video with Augmented Reality
Creating a Custom Progress Bar with Images
Uialertview Is Not Working in Swift
Sort Array by Distance Near User Location from Firebase
Getting the Local iPhone Number Through Sdk
Why Was Uitextfield's Text Property Changed to an Optional in Swift 2
Uiactivityviewcontroller Completion Handler Returns Success When Tweet Has Failed
I Just Want to Open Ms Outlook App and See Mailto Screen Using Url Scheme at iOS
How to Change Device Orientation Programmatically in Swift
How to Determine the Correct Altitude for an Mkmapcamera Focusing on an Mkpolygon
Cannot Get My Location to Show Up in Google Maps in iOS 8
Cannot Assign Class Instance to Its Protocol Type
Synchronizing Read/Write Access to an Instance Variable for High Performance in iOS