我在代码中使用代理并获得身份验证挑战。但在“didReceiveChallenge”被调用之前,我会收到警报,如下图所示。如果我在警报上单击“取消”,则调用“didReceiveChallenge”。
let url = NSURL(string: "http://www.google.com")
let request = NSURLRequest(URL: url!)
let config = NSURLSessionConfiguration.ephemeralSessionConfiguration()
let proxyDict:[String:AnyObject] = [kCFNetworkProxiesHTTPEnable as String: true,
hostKey as String:proxy_server,
portKey as String: proxy_port,
userNameKey as String:userName,
passwordKey as String:password]
config.connectionProxyDictionary = proxyDict as [NSObject : AnyObject]
let session = NSURLSession(configuration: config, delegate: self, delegateQueue: NSOperationQueue.mainQueue())
let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
print(data)
print(error)
print(response)
});
task.resume()
func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
let credential = NSURLCredential(user:user, password:password, persistence: .ForSession)
completionHandler(NSURLSessionAuthChallengeDisposition.UseCredential,credential)
}
我如何抑制/绕过即将来临的警报?非常感谢任何帮助。