在 UIWebView 中加载网页时出错

Error loading web in UIWebView

我正在使用 UIWebView,但无法加载 facebook。我不得不说我正在使用 xcode 7 beta 2 和 iOS 9.0 beta 4。

这是错误:

Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection."

所以,我会回答我自己的问题。正如 CSmith 回答的那样,苹果希望我们使用 App Transport Security (ATS)。但是,由于还有很多人没有准备好,我将展示避免它的方法。在您的 info.plist 中添加以下内容:

<key>NSAppTransportSecurity</key>
<dict>
  <!--Include to allow all connections (DANGER)-->
  <key>NSAllowsArbitraryLoads</key>
  <true/>
</dict>

相关:

在 iOS 9(和 OS X 10.11)中,应用程序不应发出不受保护的 HTTP 请求,因此默认情况下它们是禁用的。您还应该使用具有前向保密性的 TLS 1.2。 RC4 和 SHA-1 证书签名被禁用,RSA 的密钥长度至少应为 2048 位(EC 为 256 位)。

如果您真的想使用 http 或不太安全的 https,您可以在应用的 info.plist 文件中定义例外。

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
  <true/>
  <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>