ios - swift 2 : NSData(contentsOfURL:url) returning nil

标签 ios json xcode foundation swift2

我有一个包含 JSON 文件的网页地址。我正在尝试在 Swift 2 中以 NSDictionary 的形式访问 JSON 文件。每次我调用 NSData(contentsOfURL:url!) 时,urlNSURL 的类型吗? ,它返回 nil。我正在使用 Xcode 7 Beta,该项目最初是在 Xcode 6 中制作的。

以下代码导致了问题。

let url = NSURL(string:myurl) // myurl is the webpage address.
let data = NSData(contentsOfURL:url!) // the bit that returns nil
// data is set to nil
// I would perform NSJSONSerialization.JSONObjectWithData later.

令我困惑的是,当我在终端中使用 swift 键入相同的代码时尝试完全相同的操作时,常量 data 未设置为 nil。我试过重新启动 Mac,但没有用。我尝试重新安装 Xcode,但没有用。

这就是当我使用 swift 关键字向终端输入以下代码时发生的情况。

$> swift
......
Welcome to Apple Swift version 2.0 (700.0.38.1 700.0.53). Type :help for assistance. 

1> import Foundation
2> var urlstr = "http://mywebsiteaddress/jsonfile.json"
3> var nsurl = NSURL(string:urlstr)
nsurl: NSURL? = "http://mywebsiteaddress/jsonfile.json"{
    ObjectiveC.NSObject = {...}
}
4> var nsdata = NSData(contentsOfURL:nsurl!)
nsdata: NSData? = 5925 bytes {
    ObjectiveC.NSObject = {...}
}
5> print(nsdata)
Optional(<Some Values..........>)

当我在终端中尝试时,它确实有效。谁能帮我解决这个问题??

最佳答案

我希望它能在终端中运行,因为您在这里看到的可能不是 Swift 或 Cocoa Touch 中的错误,而是 iOS 9 中名为 App Transport Security 的新功能的副作用。 .这意味着默认情况下,iOS 不允许您向未受 SSL 保护的服务器发出请求。

引用自链接:

App Transport Security (ATS) lets an app add a declaration to its Info.plist file that specifies the domains with which it needs secure communication. ATS prevents accidental disclosure, provides secure default behavior, and is easy to adopt. You should adopt ATS as soon as possible, regardless of whether you’re creating a new app or updating an existing one.

If you’re developing a new app, you should use HTTPS exclusively. If you have an existing app, you should use HTTPS as much as you can right now, and create a plan for migrating the rest of your app as soon as possible.

要解决此问题,您可以编辑 info.plist 文件以在逐个域的基础上创建异常(exception),或完全禁用 App Transport Security。这是引用自 CFNetwork SSLHandshake failed iOS 9 Beta 1 的示例.

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
    <key>yourserver.com</key>
    <dict>
      <!--Include to allow subdomains-->
      <key>NSIncludesSubdomains</key>
      <true/>
      <!--Include to allow insecure HTTP requests-->
      <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
      <true/>
      <!--Include to specify minimum TLS version-->
      <key>NSTemporaryExceptionMinimumTLSVersion</key>
      <string>TLSv1.1</string>
    </dict>
  </dict>
</dict>

虽然我建议您实际上不要将其用作您的解决方案,而是使用 SSL 安全的 https URL。

关于ios - swift 2 : NSData(contentsOfURL:url) returning nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30848338/

相关文章:

ios - 在 Prototype Cell 中访问 UISwitch

iphone - 在 Storyboard ios 中的 UIScrollView 内滚动表格 View

javascript - 使用 JS 和 foreach 循环/追加覆盖 div

ios - 如何在 Xcode 控制台中禁用 Apptentive 日志记录?

iphone - 属性 'View; not found on object of type "x"

ios - 制作一个滑动按钮

ios - 如何使用扩展设置 viewController 搜索栏的委托(delegate)?

json - 使用 Play 2.1.1 在 JSON 中迭代和重新格式化数组

javascript - 将返回的 PHP 数据转换为 JavaScript 对象

ios - 如何将数据传递到嵌入在TableViewCell中的UICollectionView(xCode-iOS-Swift 3)