NSURL无法在Web视图-9824中加载

时间:2016-09-18 03:00:37

标签: ios swift xcode uiwebview nsurlrequest

我正在创建一个基于isbn返回书评的应用。我正在尝试在Web视图中加载Goodreads审核小部件,但我收到两个错误:

CFNetwork SSLHandshake failed (-9824)NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9824)

我尝试过其他几个网址并得到相同的结果。

以下是我调用网址的代码:

override func viewDidLoad() {
        super.viewDidLoad()

        let url = NSURL (string: "https://www.goodreads.com/api/index#book.show_by_isbn");
        let requestObj = NSURLRequest(URL: url!);
        webView.loadRequest(requestObj);
}

在info.plist中,我在Exception Domains字典中有一个goodreads.com字典。我包含在goodreads.com NSIncludesSubdomains, NSExceptionsAllowsInsecureHTTPLoads, and NSExceptionsMinimumTLSVersion(设置为TLSv1.1)

有什么明显的事我错了吗?域名错了吗?

编辑:对于稍后阅读此内容的人,我想我已经解决了这个问题。这个页面(Does App Store reject submission if NSAllowsArbitraryLoads set to YES?)帮助极大。

如果您使用NSAllowsArbitraryLoads,App Store将拒绝您的应用。但是,他们会允许NSAllowsArbitraryLoadsInWebContent

根据Apple开发者指南,

  

"将此键的值设置为YES以获得ATS政策的豁免   您的应用程序的Web视图,而不会影响ATS授权的安全性   您的NSURLSession连接。支持旧版本的iOS和   macOS(早于iOS 10),您可以使用此密钥并仍然手动配置ATS。去做   所以,将此键的值设置为YES并配置   NSAllowsArbitraryLoads地铁。如果您将此密钥添加到Info.plist中   然后,无论密钥的值如何,ATS都会忽略   NSAllowsArbitraryLoads键的值。"

3 个答案:

答案 0 :(得分:0)

尝试在project.plist中添加NSAppTransportSecurity,并检查您的网址请求是否有效。

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

enter image description here

答案 1 :(得分:0)

如果您希望允许特定域使用它,那么它比允许所有

更安全
  <key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
    <key>yourserver.com</key>
    <dict>
      <!--Include to allow subdomains-->
      <key>NSIncludesSubdomains</key>
      <true/>
      <!--Include to allow HTTP requests-->
      <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
      <true/>
      <!--Include to specify minimum TLS version-->
      <key>NSTemporaryExceptionMinimumTLSVersion</key>
      <string>TLSv1.1</string>
    </dict>
  </dict>
</dict>

修复很容易添加下面的行,它可以工作。

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

我附上了我测试过的项目

LINK TO THE PROJECT

答案 2 :(得分:0)

您还必须确保在目标中禁用了App Sandbox&gt;能力。 Xcode自动为新生成的项目启用它。