本文介绍了NSURLSession“HTTP加载失败kCFStreamErrorDomainSSL,-9813;自签名证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的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>

只需查看此链接,它也可以帮助您:



-



否则,您可以设置域例外。这是怎么做:


  1. 转到 Info.plist 文件。

  2. 添加一行 Type Dictionary Key 应用传输安全设置

  3. 添加一行类型 字典 Key 异常域

  4. 添加一行 Type 词典 域名

  5. 添加两行类型 布尔 NSExceptionAllowsInsecureHTTPLoads & NSIncludesSubdomains Value to YES





链接








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.

  1. Go to your Info.plist file.
  2. Add a row of Type Dictionary and of Key App Transport Security Settings.
  3. In this Dictionary, add a row of Type Boolean, of Key Allow Arbitrary Loads and of Value YES

-

Otherwise you can set Domain Exceptions. This is how to do :

  1. Go to your Info.plist file.
  2. Add a row of Type Dictionary and of Key App Transport Security Settings.
  3. Add a row of Type Dictionary and of Key Exception Domains.
  4. Add a row of Type Dictionary and of Key The domain.
  5. Add two rows of Type Boolean and of Key NSExceptionAllowsInsecureHTTPLoads & NSIncludesSubdomains with Value to YES.

Links

https://forums.developer.apple.com/message/5857#5857

2015 WWDC Session 711

iOS 9 HTTP Connection Error - StackOverflow

这篇关于NSURLSession“HTTP加载失败kCFStreamErrorDomainSSL,-9813;自签名证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:56