NSURLSession委托方法未调用

时间:2014-01-18 05:42:02

标签: nsurlconnection nsurl nsurlrequest nsurlsession

我创建了一个非常简单的应用程序,用于从我的Web服务器下载文本文件。我有这个与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);
        }

    }
}

谢谢!

1 个答案:

答案 0 :(得分:9)

解决!

以下代码行是罪魁祸首:

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

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

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

我觉得它只是不起作用似乎很奇怪。我原以为弹出错误。