在 Swift 中使用后台会话提交下载任务时出错

Error when submitting download task with background session in Swift

当我尝试使用以这种方式创建的后台会话提交下载 URL 时:

func backgroundSession() -> NSURLSession?{
    var session:NSURLSession?;
    var configuration:NSURLSessionConfiguration!;
    if NSURLSessionConfiguration.respondsToSelector(Selector("backgroundSessionConfigurationWithIdentifier:")){
        configuration = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("myBackgroundSessionIdentifier\(++counter)")
    }
    else{
        configuration = NSURLSessionConfiguration.backgroundSessionConfiguration("myBackgroundSessionIdentifier\(++counter)")
    }
    configuration.allowsCellularAccess = true;
    configuration.timeoutIntervalForRequest = 20.0;
    configuration.timeoutIntervalForResource = 40.0;
    configuration.HTTPMaximumConnectionsPerHost = 1;
    configuration.networkServiceType = .NetworkServiceTypeBackground;
    configuration.discretionary = false;
    let queue=NSOperationQueue.mainQueue()
    session = NSURLSession(configuration:configuration, delegate:self, delegateQueue:queue)
    return session;
}

应用崩溃:

CFNetwork SSLHandshake failed (-9810)

当我提交相关任务时:

let url=NSURL(fileURLWithPath: escapedUrlString!);
let task = backgroundSession()?.downloadTaskWithURL(url!, completionHandler: {[weak self](data, reponse, error) in
        //UIApplication.sharedApplication().applicationIconBadgeNumber = 3;
    })
task!.resume()

而如果我使用创建的标准会话执行相同操作:

 NSURLSession.sharedSession()

连接没有问题,但当然会在应用程序变为非活动状态时停止提交。

错误的根源可能是什么问题?

我丢失了对该线程的引用,但问题已以某种方式解决。