问题描述
我正在尝试将我的iOS应用程序连接到HTTPS Rails应用程序,该应用程序当前在我的网络中的本地主机上运行。我可以使用 https:// myIP:3000 / display
从我的浏览器访问该站点,也可以在命令行中使用curl请求访问该站点。我正试图通过以下方式从我的应用程序访问它:
class FirstViewController:UIViewController {
override func viewDidLoad(){
super.viewDidLoad()
// let url = NSURL(string:https:// another / Sinatra / website / feed)
let url = NSURL( string:https:// myIP:3000 / display)
let request = NSURLRequest(URL:url!)
let task = NSURLSession.sharedSession()。dataTaskWithURL(url!)
任务!.resume()
}
当我尝试访问sinatra网站我没有遇到麻烦,甚至可以使用正确的命令将JSON打印到控制台。但是,当我将url设置到我的Rails网站时,我收到以下错误。
此外,我可以告诉我的localhost Rails应用程序(在另一台机器上)没有从iOS应用程序中获取,但是从浏览器和curl命令中获取。
任何想法如何解决这个问题?
如果我理解得很好,Apple现在希望开发人员使用HTTPS和TLS 1.2进行网络通话。
临时,你可以在Info.plist文件中添加:
< key> NSAppTransportSecurity< / key>
< dict>
< key> NSAllowsArbitraryLoads< / key>
< true />
< / dict>
只需查看此链接,它也可以帮助您:
-
否则,您可以设置域例外
。这是怎么做:
- 转到
Info.plist
文件。 - 添加一行 Type
Dictionary
和 Key应用传输安全设置
。 - 添加一行类型
字典
和 Key异常域
。 - 添加一行 Type
词典
和键域名
。 - 添加两行类型
布尔
和键NSExceptionAllowsInsecureHTTPLoads
&NSIncludesSubdomains
Value toYES
。
链接
I'm trying to connect my iOS app to an HTTPS Rails app which is currently run on a local host in my network. I can access the site from my browser with https://myIP:3000/display
as well as in the command line with a curl request. I'm trying to access it from my app using:
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//let url = NSURL(string: "https://another/Sinatra/website/feed")
let url = NSURL(string: "https://myIP:3000/display")
let request = NSURLRequest(URL: url!)
let task = NSURLSession.sharedSession().dataTaskWithURL(url!)
task!.resume()
}
When I try to access the sinatra website I have no trouble and can even print out the JSON to the console with the correct commands. However, when I set url to my Rails website I get the following error.
In addition, I can tell that my localhost Rails app (on the other machine) is not pinged from the iOS app, but is pinged from the browser and curl command.
Any ideas how to fix this?
If I understood well, Apple now wants the developers to use HTTPS and TLS 1.2 for network calls.
Temporary, you can add this in your "Info.plist" file :
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Just have a look to this link, it may also help you : http://ste.vn/2015/06/10/configuring-app-transport-security-ios-9-osx-10-11/
--
Update : Following some comments the above solution is not anymore working. Here is an updated solution.
- Go to your
Info.plist
file. - Add a row of Type
Dictionary
and of KeyApp Transport Security Settings
. - In this Dictionary, add a row of Type
Boolean
, of KeyAllow Arbitrary Loads
and of ValueYES
-
Otherwise you can set Domain Exceptions
. This is how to do :
- Go to your
Info.plist
file. - Add a row of Type
Dictionary
and of KeyApp Transport Security Settings
. - Add a row of Type
Dictionary
and of KeyException Domains
. - Add a row of Type
Dictionary
and of KeyThe domain
. - Add two rows of Type
Boolean
and of KeyNSExceptionAllowsInsecureHTTPLoads
&NSIncludesSubdomains
with Value toYES
.
Links
https://forums.developer.apple.com/message/5857#5857
iOS 9 HTTP Connection Error - StackOverflow
这篇关于NSURLSession“HTTP加载失败kCFStreamErrorDomainSSL,-9813;自签名证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!