4

我创建了一个非常简单的应用程序来从我的 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);
        }

    }
}

谢谢!

4

1 回答 1

9

解决了!

以下代码行是罪魁祸首:

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

原来它也需要 http:// ,所以这个有效

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

对我来说仍然很奇怪,它只是没有用。我本来预计会弹出一个错误。

于 2014-01-18T15:42:01.810 回答