I'm trying to use Swift's NSURLRequest
and NSURLConnection
to communicate with the Riot Games (League of Legends) API. I seem to be having issues not only with Riot URLs but with all HTTPS URLs.
I have the following code in a Swift Playground:
func doGET(url: String) {
if let urlhandle = NSURL(string: url) {
var request = NSURLRequest(URL: urlhandle)
var response: NSURLResponse?
var error: NSErrorPointer = nil
var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: error)
println("\n\(url)\n")
println("\(response)\n\n")
} else {
println("Bad URL: \(url)")
}
}
doGET("http://google.com")
doGET("https://na.api.pvp.net/api/lol/na/v1.2/champion/1?api_key=\(apikey)")
doGET("https://google.com")
I'm getting the following errors/output:
http://google.com
Optional(<NSHTTPURLResponse: 0x7fcff2443d60> { URL: http://www.google.com/ } { status code: 200, headers {
"Accept-Ranges" = none;
"Alternate-Protocol" = "80:quic,p=0.08";
"Cache-Control" = "private, max-age=0";
"Content-Type" = "text/html; charset=ISO-8859-1";
Date = "Wed, 25 Feb 2015 23:13:45 GMT";
Expires = "-1";
Server = gws;
"Transfer-Encoding" = Identity;
Vary = "Accept-Encoding";
"X-Frame-Options" = SAMEORIGIN;
"X-XSS-Protection" = "1; mode=block";
} })
2015-02-25 16:13:45.357 URL Requests[36397:3779864] NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9807)
https://na.api.pvp.net/api/lol/na/v1.2/champion/1?api_key=<redacted>
nil
2015-02-25 16:13:45.402 URL Requests[36397:3779864] NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9807)
https://google.com
nil
As you can see, the request succeeds on HTTP URLs but fails on secure HTTPS URLs. How can I allow the use of HTTPS?