NSURLSession delegate not called
Did you try to implement
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
to see if some error is returned?
NSURLSessionDataTask delegate methods are not called
Use the method
+ (NSURLSession *)sessionWithConfiguration:(NSURLSessionConfiguration *)configuration
delegate:(id<NSURLSessionDelegate>)delegate
delegateQueue:(NSOperationQueue *)queue
to set the delegate properly.
Update:
And you have to call the completion handler of didReceiveResponse
completionHandler
A completion handler that your code calls to continue the transfer,
passing a constant to indicate whether the transfer should continue as
a data task or should become a download task.
• If you passNSURLSessionResponseAllow
, the task continues normally.
• If you passNSURLSessionResponseCancel
, the task is canceled.
• If you passNSURLSessionResponseBecomeDownload
as the disposition, your delegate’sURLSession:dataTask:didBecomeDownloadTask:
method is called to provide you with the new download task that supersedes the current task.
For example
- (void) URLSession:(NSURLSession *)session
dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
completionHandler(NSURLSessionResponseAllow);
}
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.
Delegate methods not called in implementation of NSURLSessionData
You're calling dataTaskWithURL
. This is a data task method. Data tasks have a protocol called NSURLSessionDataDelegate
. Override those methods, e.g. URLSession:dataTask:...
.
didWriteData
is called when using download methods, such as downloadTaskWithURL:, and you're not calling a download method.
If you look at https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSession_class/#//apple_ref/occ/instm/NSURLSession/downloadTaskWithURL:, in the contents area on the left you'll see that data, download, and upload tasks are broken out separately. Each has their corresponding delegate methods.
NSURLSession delegate methods not called
SOLVED!
The following line of code was the culprit:
NSString *fileURL = @"www.mywebsite.com/utility/file.txt";
Turns out it needed http:// in there as well, so this one works
NSString *fileURL = @"http://www.mywebsite.com/utility/file.txt";
It still seems weird to me that it just didn't work. I would have expected an error to popup.
Related Topics
Adjusting Uiview Height Inside Uistackview Control
Swiftui Won't Display Custom Font
Uitapgesturerecognizer on Uiimageview Within Uitablevlewcell Not Getting Called
Is Wkwebview Designed as a Replacement of Uiwebview
Arkit Session Paused and Not Resuming
Swift - Downcast Nsurlresponse to Nshttpurlresponse in Order to Get Response Code
Objective-C Rabbitmq Client Not Publishing Messages to Queue
Uicollectionviewcell Dynamic Height W/Two Dynamic Labels & Auto Layout
iOS 5 Gm: <Error>: More Than Maximum 5 Filtered Album Lists Trying to Register. This Will Fail
Healthkit Authorisation Status Is Always 1
Uicontrolstate.Normal Is Unavailable
How to Implement Tableview Inside Tableview Cell in Swift 3
Why Isn't Preferredcontentsize Used by iPhone 6 Plus Landscape
iOS How to Dismiss Uialertview with One Tap Anywhere
How to Run Multiple iOS Simulators at Once
What Is the Swift Preprocessor Equivalent to iOS Version Check Comparison