gpt4 book ai didi

nsurlconnection - 未调用 NSURLSession 委托(delegate)方法

转载 作者:行者123 更新时间:2023-12-04 23:02:01 28 4
gpt4 key购买 nike

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

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

我的服务器受密码保护,因此我需要使用基本的 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];
}

我实现的委托(delegate)方法是:
-(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);
}

}
}

谢谢!

最佳答案

解决了!

以下代码行是罪魁祸首:

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

原来它也需要 http://,所以这个有效
 NSString *fileURL = @"http://www.mywebsite.com/utility/file.txt";

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

关于nsurlconnection - 未调用 NSURLSession 委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21200521/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com