Nsurlsession Delegates Not Called

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 pass NSURLSessionResponseAllow, the task continues normally.

• If you pass NSURLSessionResponseCancel, the task is canceled.

• If you pass NSURLSessionResponseBecomeDownload as the disposition, your delegate’s URLSession: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



Leave a reply



Submit