未调用 NSURLSession 委托方法 [英] NSURLSession delegate methods not called

查看:56
本文介绍了未调用 NSURLSession 委托方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个非常简单的应用程序来从我的网络服务器下载文本文件.我在 NSURLConnection 上完美地工作,但我正在尝试迁移到 NSURLSession.

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.

我的服务器受密码保护,所以我需要使用基本的 http 身份验证来访问文件,但是当 didReceiveChallenge 方法从未被调用时.

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.

代码行 [getFileTask resume] 似乎对任何事情都没有影响.

The line of code [getFileTask resume] seems to have no effect on anything.

我的设置如下:

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

从 viewDidLoad 调用以下方法:

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];
}

我实现的委托方法是:

-(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);
        }

    }
}

谢谢!

推荐答案

SOLVED!

以下代码行是罪魁祸首:

The following line of code was the culprit:

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

原来它也需要 http://,所以这个工作

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.

这篇关于未调用 NSURLSession 委托方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆