Sco*_*ter 4 nsurl nsurlconnection nsurlrequest nsurlsession
我创建了一个非常简单的应用程序来从我的Web服务器下载文本文件.我有这个与NSURLConnection完美配合,但我正在尝试迁移到NSURLSession.
我遇到的问题是没有调用任何委托方法.
我的服务器受密码保护,因此我需要使用基本的http身份验证来访问该文件,但是从未调用didReceiveChallenge方法.
代码行[getFileTask resume]似乎对任何事都没有影响.
我的设置如下:
@interface ViewController : UIViewController <NSURLSessionDelegate, NSURLSessionDownloadDelegate, NSURLSessionTaskDelegate>
{
NSURLSession *session;
}
Run Code Online (Sandbox Code Playgroud)
从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];
}
Run Code Online (Sandbox Code Playgroud)
我实施的委托方法是:
-(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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
解决了!
以下代码行是罪魁祸首:
NSString *fileURL = @"www.mywebsite.com/utility/file.txt";
Run Code Online (Sandbox Code Playgroud)
事实证明它在那里也需要http://,所以这个有效
NSString *fileURL = @"http://www.mywebsite.com/utility/file.txt";
Run Code Online (Sandbox Code Playgroud)
它似乎很奇怪,它只是没有用.我原以为弹出错误.
| 归档时间: |
|
| 查看次数: |
10240 次 |
| 最近记录: |