以下代码导致问题.
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.@H_502_5@令我困惑的是,当我在终端中使用swift键入相同的代码时,我尝试完全相同的事情,常量数据没有设置为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..........>)@H_502_5@
解决方法@H_404_16@
我期望它在终端上工作,因为你在这里看到的可能不是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文件,以域为基础进行例外处理,或完全禁用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>@H_502_5@
虽然我建议您不要将此作为您的解决方案,而是使用SSL安全的https URL.
报价从链接:
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文件,以域为基础进行例外处理,或完全禁用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>@H_502_5@虽然我建议您不要将此作为您的解决方案,而是使用SSL安全的https URL.