xcode - Swift Playground 中的 NSURLErrorFailingURLPeerTrustErrorKey

标签 xcode swift https nsurlsession swift-playground

当 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 + 1View > Utilities > 从菜单中显示文件检查器

关于xcode - Swift Playground 中的 NSURLErrorFailingURLPeerTrustErrorKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27198000/

相关文章:

ios - Swift 中的 Firebase 嵌套查询无法正常工作

ASP.NET SSL 身份验证票证安全吗?

https - 证书中的主机名与 :<>! =<> WSO2 APIM 不匹配

ssl - 验证对等点与发送 https post 分开

objective-c - 如何处理仅从键盘在 ObjC 源代码中键入嵌套消息调用?

c - {{a,b,c},{d,e,f}} 和 {{g,h,i},{j,k,i},{l,m,n}} 卷积的 ListCorrelate vDSP 等价物

ios - segue.identifier 在 if block 中不起作用

ios - MFMailComposeViewController 获取邮件正文

ios - 在键盘出现之前获取键盘高度

swift - 如何从 Swift 中的数组中删除给定值的元素