【问题标题】:NSURLSession delegate methods not called未调用 NSURLSession 委托方法
【发布时间】:2014-02-07 15:31:05
【问题描述】:

我创建了一个非常简单的应用程序来从我的网络服务器下载文本文件。我可以与 NSURLConnection 完美配合,但我正在尝试迁移到 NSURLSession。

我遇到的问题是没有调用任何委托方法。

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

这行代码 [getFileTask resume] 似乎对任何东西都没有影响。

我的设置如下:

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

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

    }
}

谢谢!

【问题讨论】:

    标签: nsurlconnection nsurl nsurlrequest nsurlsession


    【解决方案1】:

    解决了!

    以下代码行是罪魁祸首:

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

    原来那里也需要 http://,所以这个可以工作

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

    对我来说,它仍然不起作用,这仍然很奇怪。我本来预计会弹出一个错误。

    【讨论】:

    • 仅供参考,如果您使用 completion handler 创建会话,则不会调用委托方法。
    • @HwanghoKim 非常感谢!我有一个完成处理程序,并且从未调用过委托方法。我快疯了!
    • @HwanghoKim,如果您使用完成处理程序创建会话,则不会调用委托方法是不正确的。我有这种方式,而且效果很好。没有文件证实你的说法。如果没有调用这些方法,那么您的代码中肯定有其他问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    相关资源
    最近更新 更多