我尝试通过此代码通过HTTP执行GET请求:
func httpGet(request: NSURL, callback: (String, String) -> Void) {
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(request) {
(data,response,error) -> Void in
if (error != nil) {
callback("", error!.localizedDescription)
} else {
let result = NSString(data: data!, encoding: NSASCIIStringEncoding)
callback(result! as String, "")
}
}
task.resume()
}
func updateIssueList () {
httpGet(NSURL(string: "http://www.google.com")!) {
(data, error) -> Void in
print("error: \(error)")
print("data: \(data)")
}
}
它已正确执行,并且无错误地返回数据。但是当我将协议更改为HTTPS(https://www.google.com“)时,请求失败。当我尝试在设备上运行应用程序(iPhone 4s)时,我收到了
Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo=0x17579730 {NSErrorFailingURLStringKey=https://www.google.com, NSErrorFailingURLKey=https://www.google.com, NSLocalizedDescription=The network connection was lost., NSUnderlyingError=0x1758e470 "The network connection was lost."}
在模拟器上,两种变体都能正常工作。我做错了什么?