Invalid NSURLSession delegate methods

I created a very simple application to download a text file from my web server. I work fine with NSURLConnection, but try to switch to NSURLSession instead.

The problem I am facing is that none of the delegate methods are called.

My server is password protected, so I need to use basic HTTP authentication to access the file, but when the didReceiveChallenge method is never called.

The line of code [getFileTask resume] does not seem to affect anything.

My setup is as follows:

@interface ViewController : UIViewController <NSURLSessionDelegate, NSURLSessionDownloadDelegate, NSURLSessionTaskDelegate>
{
   NSURLSession *session;
}

The following method is called in 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 that I followed are as follows:

-(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!

+4
1

!

:

 NSString *fileURL = @"www.mywebsite.com/utility/file.txt";

, http://,

 NSString *fileURL = @"http://www.mywebsite.com/utility/file.txt";

, . .

+9

All Articles