xcode Swift 2:NSData(contentsOfURL:url) 返回 nil
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30848338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift 2 : NSData(contentsOfURL:url) returning nil
提问by Minjae Kwak
I have a webpage address which contains a JSON file. I'm trying to access the JSON file as NSDictionary in Swift 2. Every time I call NSData(contentsOfURL:url!)
where url
is type of NSURL?
, it returns nil
. I'm using Xcode 7 Beta, and the project was first made in Xcode 6.
我有一个包含 JSON 文件的网页地址。我试图在 Swift 2 中以 NSDictionary 的形式访问 JSON 文件。每次我调用 NSData(contentsOfURL:url!)
whereurl
是 type of 时NSURL?
,它都会返回nil
. 我使用的是 Xcode 7 Beta,该项目最初是在 Xcode 6 中制作的。
The following code is causing the problem.
以下代码导致了问题。
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.
What is confusing me is that when I try the exact same thing when I type the same code in Terminal using swift
, the constant data
is not set to nil. I have tried rebooting the Mac, and it didn't work. I tried reinstalling Xcode, and it didn't work.
让我感到困惑的是,当我在终端中使用 键入相同的代码时尝试完全相同的事情时swift
,常量data
未设置为 nil。我试过重新启动 Mac,但没有奏效。我尝试重新安装Xcode,但没有用。
This is what happens when I type the following code to the Terminal, using swift
keyword.
当我使用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..........>)
When I try in the Terminal it definitely worked. Can anyone help me solve the problem??
当我在终端中尝试时,它确实有效。谁能帮我解决问题??
回答by Mick MacCallum
I would expect it to work in the terminal, since what you're seeing here is likely not a bug in Swift or Cocoa Touch, but the side effects of a new feature in iOS 9 called App Transport Security. What this means is that by default, iOS will not permit you to make requests servers not secured with SSL.
我希望它在终端中工作,因为您在这里看到的可能不是 Swift 或 Cocoa Touch 中的错误,而是 iOS 9 中名为App Transport Security的新功能的副作用。这意味着默认情况下,iOS 不允许您请求服务器未使用 SSL 进行保护。
Quote from link:
引自链接:
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.
应用传输安全 (ATS) 允许应用向其 Info.plist 文件添加声明,指定需要安全通信的域。ATS 可防止意外泄露,提供安全的默认行为,并且易于采用。无论您是创建新应用还是更新现有应用,都应尽快采用 ATS。
如果您正在开发新应用程序,则应专门使用 HTTPS。如果您有一个现有的应用程序,您应该立即尽可能多地使用 HTTPS,并尽快制定迁移应用程序其余部分的计划。
To fix this, you can edit your info.plist file to make exceptions on a domain by domain basis, or disable App Transport Security entirely. Here's an example quoted from CFNetwork SSLHandshake failed iOS 9 Beta 1.
要解决此问题,您可以编辑 info.plist 文件以逐个域设置例外,或完全禁用 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>
Although I advise that you don't actually use this as your solution and instead use an SSL secured https URL instead.
尽管我建议您实际上不要将其用作解决方案,而是使用 SSL 保护的 https URL。