Swift/https: NSURLSession/NSURLConnection HTTP 加载失败
Swift/https: NSURLSession/NSURLConnection HTTP load failed
不幸的是,今天早上我的 XCode 更新到了版本 7,而我使用 http 开发的 iOS 应用现在需要 https。因此,按照许多教程,我配置了我的 MAMP 服务器,以便使用 https/ssl 创建一个虚拟证书。现在在我的 iOS 应用程序中,URL 如下所示:
static var webServerLoginURL = "https://localhost:443/excogitoweb/mobile/loginM.php"
static var webServerGetUserTasks = "https://localhost:443/excogitoweb/mobile/handleTasks.php"
static var webServerGetUsers = "https://localhost:443/excogitoweb/mobile/handleUsers.php"
static var webServerGetProjects = "https://localhost:443/excogitoweb/mobile/handleProjects.php"
如果我尝试在我的浏览器中访问它们,它们工作正常。
我习惯于使用 NSURLSession.sharedSession().dataTaskWithRequest() 访问数据库和 php 文件,这现在引发了标题错误。例如,这是引发错误的行:
if let responseJSON: [[String: String]] = (try? NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())) as? [[String: String]] {
...
}
这是完整的错误信息:
2015-09-21 16:41:48.354 ExcogitoWeb[75200:476213] CFNetwork SSLHandshake failed (-9824)
2015-09-21 16:41:48.355 ExcogitoWeb[75200:476213] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9824)
fatal error: unexpectedly found nil while unwrapping an Optional value
我想知道如何解决这个问题。我在这里阅读了一些有用的答案,但还有很多事情我仍然不明白,如果有人愿意 help/explain 我将不胜感激。
fatal error: unexpectedly found nil while unwrapping an Optional value
通常意味着你做的事情不太好,通过查看你的 if
if let responseJSON: [[String: String]] = (try? NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())) as? [[String: String]] {
我可以看到有一个 data!
,但是那个数据对象是 nil。你真的应该在使用它们之前解开可选值,尤其是在处理远程数据时。
那你网络有问题,这可能与ATS
Apple在iOS9.
中添加有关
查看另一个关于如何暂时禁用 ATS 的答案。
编辑:我现在看到您已将 ssl 添加到您的本地主机,这很好。然而,这还不足以让 ATS 工作,因为它需要 TLS 1.2 而不是自签名证书。
将此添加到您应用的 Info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
真是个老问题,但我想我会回答。这通常发生在您尝试打开一个 http 对象并且它返回 nil 时。
确保打开 url 并为其指定默认值。
示例:
let jsonURL = "your https link to your json"
guard let url = URL(string: jsonURL) else { return } // dont force unwrap
希望对您有所帮助!
不幸的是,今天早上我的 XCode 更新到了版本 7,而我使用 http 开发的 iOS 应用现在需要 https。因此,按照许多教程,我配置了我的 MAMP 服务器,以便使用 https/ssl 创建一个虚拟证书。现在在我的 iOS 应用程序中,URL 如下所示:
static var webServerLoginURL = "https://localhost:443/excogitoweb/mobile/loginM.php"
static var webServerGetUserTasks = "https://localhost:443/excogitoweb/mobile/handleTasks.php"
static var webServerGetUsers = "https://localhost:443/excogitoweb/mobile/handleUsers.php"
static var webServerGetProjects = "https://localhost:443/excogitoweb/mobile/handleProjects.php"
如果我尝试在我的浏览器中访问它们,它们工作正常。 我习惯于使用 NSURLSession.sharedSession().dataTaskWithRequest() 访问数据库和 php 文件,这现在引发了标题错误。例如,这是引发错误的行:
if let responseJSON: [[String: String]] = (try? NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())) as? [[String: String]] {
...
}
这是完整的错误信息:
2015-09-21 16:41:48.354 ExcogitoWeb[75200:476213] CFNetwork SSLHandshake failed (-9824)
2015-09-21 16:41:48.355 ExcogitoWeb[75200:476213] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9824)
fatal error: unexpectedly found nil while unwrapping an Optional value
我想知道如何解决这个问题。我在这里阅读了一些有用的答案,但还有很多事情我仍然不明白,如果有人愿意 help/explain 我将不胜感激。
fatal error: unexpectedly found nil while unwrapping an Optional value
通常意味着你做的事情不太好,通过查看你的 if
if let responseJSON: [[String: String]] = (try? NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())) as? [[String: String]] {
我可以看到有一个 data!
,但是那个数据对象是 nil。你真的应该在使用它们之前解开可选值,尤其是在处理远程数据时。
那你网络有问题,这可能与ATS
Apple在iOS9.
查看另一个关于如何暂时禁用 ATS 的答案。
编辑:我现在看到您已将 ssl 添加到您的本地主机,这很好。然而,这还不足以让 ATS 工作,因为它需要 TLS 1.2 而不是自签名证书。
将此添加到您应用的 Info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
真是个老问题,但我想我会回答。这通常发生在您尝试打开一个 http 对象并且它返回 nil 时。
确保打开 url 并为其指定默认值。
示例:
let jsonURL = "your https link to your json"
guard let url = URL(string: jsonURL) else { return } // dont force unwrap
希望对您有所帮助!