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.
![Scooter](https://i.stack.imgur.com/2eMZW.jpg?s=256&g=1)
Scooter
Retired Naval Aviator, now working as a Senior Software Engineer with an emphasis on iOS.
Updated on June 14, 2022Comments
-
Scooter about 2 years
I have created a very simple app to download a text file from my web server. I have this working perfectly with NSURLConnection, but am trying to migrate over to NSURLSession instead.
The issue I am having is that none of the delegate methods are being called.
My server is password protected so I need to use the basic http authentication to access the file, but when the didReceiveChallenge method is never called.
The line of code [getFileTask resume] seems to have no effect on anything.
My setup is as follows:
@interface ViewController : UIViewController <NSURLSessionDelegate, NSURLSessionDownloadDelegate, NSURLSessionTaskDelegate> { NSURLSession *session; }
The following method is called from viewDidLoad:
-(void)setUpTheNetworking { NSString *fileURL = @"www.mywebsite.com/utility/file.txt"; NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; sessionConfig.allowsCellularAccess = YES; sessionConfig.timeoutIntervalForRequest = 10; sessionConfig.timeoutIntervalForResource = 10; sessionConfig.HTTPMaximumConnectionsPerHost = 1; session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil]; NSURLSessionDownloadTask *getFileTask = [session downloadTaskWithURL:[NSURL URLWithString:fileURL]]; [getFileTask resume]; }
The delegate methods I have implemented are:
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite { NSLog(@"Here we go"); } -(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes { NSLog(@"Here we go"); } -(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location { NSLog(@"Here we go"); } - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler { if (challenge.previousFailureCount == 0) { NSURLCredentialPersistence persistence = NSURLCredentialPersistenceForSession; NSURLCredential *credential = [NSURLCredential credentialWithUser:user password:@password persistence:persistence]; completionHandler(NSURLSessionAuthChallengeUseCredential, credential); } else { // handle the fact that the previous attempt failed NSLog(@"%s: challenge.error = %@", __FUNCTION__, challenge.error); completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil); } } - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler { { if (challenge.previousFailureCount == 0) { NSURLCredential *credential = [NSURLCredential credentialWithUser:user password:password persistence:NSURLCredentialPersistenceForSession]; completionHandler(NSURLSessionAuthChallengeUseCredential, credential); } else { NSLog(@"%s; challenge.error = %@", __FUNCTION__, challenge.error); completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil); } } }
Thanks!
-
Hwangho Kim over 8 yearsFYI, if you create a session with completion handler, delegate methods will not be called.
-
Luis Valdés over 8 years@HwanghoKim Thank you so much! I had a completion handler and the delegate methods were never called. I was going crazy!
-
Stephan over 8 years@HwanghoKim, it is NOT true that delegate methods will not be called if you create a session with completion handler. I have it this way and it works perfectly. No documentation confirms your statement. There has to be something else wrong in your code if the methods aren't called.
-
jungledev about 8 years