【问题标题】:NSURLSession authentication and downloading file in backgroundNSURLSession 认证和后台下载文件
【发布时间】:2014-02-04 06:37:39
【问题描述】:

在 iPad 应用程序中,我需要连接到服务器并下载使用自签名 SSL 证书且在后台模式下带有 NSURLSession 对象的文件:

static NSString *const URL = @"https://...";

- (void)testBackgroundDownloadTask {
    if (self.downloadTask) {
        return self;
    }
    self.session = [self backgroundSession];
    NSURL *downloadURL = [NSURL URLWithString:URL];
    NSURLRequest *request = [NSURLRequest requestWithURL:downloadURL];
    self.downloadTask = [self.session downloadTaskWithRequest:request];
    [self.downloadTask resume];
}

- (NSURLSession *)backgroundSession{
    static NSURLSession *sess = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:URL];
        // Default configuration: working perfectly
        //NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    configuration.sessionSendsLaunchEvents = YES;
    //configuration.TLSMinimumSupportedProtocol = kSSLProtocolAll;
    configuration.networkServiceType = NSURLNetworkServiceTypeBackground;
        sess = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
    });
    return sess;
}

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {
    NSURLProtectionSpace *protectionSpace = challenge.protectionSpace;
    NSString *authMethod = protectionSpace.authenticationMethod;

    if ([authMethod isEqualToString:NSURLAuthenticationMethodClientCertificate]) {
        // obtain challenge, it working with NSURLSession in default session config 
        [self.challengeHandler handleChallenge:challenge onCompletion:^(id obj) {
            NSURLCredential *c = (NSURLCredential *)obj;
            if (c) {
                [challenge.sender useCredential:c forAuthenticationChallenge:challenge];
                completionHandler(NSURLSessionAuthChallengeUseCredential, c);
            }
            else {
                [challenge.sender cancelAuthenticationChallenge:challenge];
            }
        }];
    }
    else if ([authMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
    }
}

如果我使用defaultSessionConfiguration,我的应用程序在调用didReceiveChallenge 方法后执行成功下载文件(调用NSURLSesionDownloadDelegate 方法

– URLSession:downloadTask:didResumeAtOffset:expectedTotalBytes: 
– URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite: 
– URLSession:downloadTask:didFinishDownloadingToURL: 

但是我使用backgroundSessionConfiguration,在调用didReceiveChallenge之后其他委托的方法没有被调用(文件没有下载和didCompleteWithError没有被调用) 有关如何解决此问题的任何想法?

【问题讨论】:

  • 目前不可用。如果您有开发者帐户,可以搜索 Apple Developer Forums 上的一些讨论。建议您向 Apple 提交错误报告,并说明它如何影响您的产品。 developer.apple.com/bug-reporting

标签: ios nsurlsession


【解决方案1】:
  1. 您是否在后台对此进行测试?

  2. 在您的NSURLSessionDownloadDelegate 上,您应该会看到实施时得到的结果

    - (void)URLSession:(NSURLSession *)session 
        task:(NSURLSessionTask *)task 
        didCompleteWithError:(NSError *)error
    {
        NSLog(@"%s %@ %@", __PRETTY_FUNCTION__, task.response, error);
    }    
    
  3. 1234563堆栈弹出时的后台进程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多