3

我有一个 NSURLSessionDownloadTask 可以正常工作一段时间,但今天它停止工作并调用了该func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential!) -> Void)方法。如果我尝试使用 SSL 下载图像,它似乎会尝试挑战,如果不使用 SSL,它工作得很好。我不确定如何处理这个问题,因为我不确定我会为挑战提供什么凭据——这对于 iOS 上的网络来说是相当新的。我应该如何进行?

func downloadProfilePicture(path: String) {
        println("1")
        let filePath: NSString = NSString(string: "https://www.domain.com/uploads/\(path).png")

        let session = NSURLSession(configuration: .defaultSessionConfiguration(), delegate: self, delegateQueue: nil)
        let downloadTask = session.downloadTaskWithURL(NSURL(string: filePath)!)

        downloadTask.resume()
        println("2")
    }

func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential!) -> Void) {
        println("6")
        println(challenge.protectionSpace.description)
    }

func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {

    println("3")
    var image: UIImage = UIImage(data: NSData(contentsOfURL: location)!)!

    dispatch_async(dispatch_get_main_queue(), {
        self.imageView.image = image
        println("4")
    })
}

安慰:

1
2
6
<NSURLProtectionSpace: 0x7fcc5a4b4ae0>: Host:www.domain.com, Server:https, Auth-Scheme:NSURLAuthenticationMethodServerTrust, Realm:(null), Port:443, Proxy:NO, Proxy-Type:(null)

注意:我已经有一段时间没有更改我的 Web 服务器上的设置了,SSL 证书仍然有效。

4

0 回答 0