当 URL 为 URL 时,底部的 playground 代码按预期工作
http://www.apple.com
当 URL 为
时失败https://www.apple.com
NSURLErrorFailingURLPeerTrustErrorKey
我猜是sharedSession和https有问题吧?有什么建议吗?
// Playground - noun: a place where people can play
import Foundation
import XCPlayground
// allow the asynchronous task to continue, set timeout in console
XCPSetExecutionShouldContinueIndefinitely()
let plainURL = NSURL(string: "https://www.apple.com/")
var session = NSURLSession.sharedSession()
func firstHandler(data:NSData!, response:NSURLResponse!, error: NSError!) {
if let err = error {
println("WTF error: \(error), \(error.userInfo)")
}
else {
println("No error!")
print(NSString(data: data, encoding: NSUTF8StringEncoding)!)
}
}
var plainTask:NSURLSessionDataTask = session.dataTaskWithURL(plainURL!, completionHandler: firstHandler)
plainTask.resume()
最佳答案
“普通”iOS Playground 不支持 HTTP 请求:
来自 Xcode release notes :
iOS Playgrounds now support displaying animated views with the XCPShowView() XCPlayground API. This capability is disabled by default; it can be enabled by checking the "Run in Full Simulator" setting in the Playground Settings inspector.
When the capability is enabled, running the playground causes the iOS Simulator application to launch and run the playground in the full simulator. This capability is also required for other functionality that fails without the full simulator, such as NSURLConnection http requests. Running in the full iOS Simulator is slower than running in the default mode. (18282806)
尝试使用“在完整模拟器中运行”模式或 OS X Playground。它会成功。
要显示检查器:option + command + 1 或 View
> Utilities
> 从菜单中显示文件检查器
。
关于xcode - Swift Playground 中的 NSURLErrorFailingURLPeerTrustErrorKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27198000/