以下代码导致问题.
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键入相同的代码时,我尝试完全相同的事情,常量数据没有设置为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..........>)
当我在终端尝试的时候肯定有效果.任何人都可以帮我解决问题吗?
解决方法
报价从链接:
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>
虽然我建议您不要将此作为您的解决方案,而是使用SSL安全的https URL.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。