ios 应用传输安全 Xcode 7 beta 6

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32427300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:31:57  来源:igfitidea点击:

App Transport Security Xcode 7 beta 6

iosxcodeswift

提问by Alan Scarpa

I'm currently working on Xcode 7 beta 6. I'm trying to send a "DELETE" request to http://mySubdomain.herokuapp.com

我目前正在研究Xcode 7 beta 6。我正在尝试向http://mySubdomain.herokuapp.com发送“删除”请求

The error I receive is:

我收到的错误是:

App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
Error making API call: Error Domain=NSURLErrorDomain Code=-1022 The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.
NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection., NSUnderlyingError=0x796f7ef0 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "(null)"}}

App Transport Security 已阻止明文 HTTP (http://) 资源加载,因为它不安全。可以通过应用程序的 Info.plist 文件配置临时异常。
调用 API 时出错:Error Domain=NSURLErrorDomain Code=-1022 无法加载资源,因为应用传输安全策略要求使用安全连接。
NSLocalizedDescription=无法加载资源,因为应用传输安全策略要求使用安全连接。, NSUnderlyingError=0x796f7ef0 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "(null)"}}

In my actual API call I put "https" instead of "http" and that actually worked for my POST requests. But the DELETE request throws the above error.

在我的实际 API 调用中,我使用了“https”而不是“http”,这实际上适用于我的 POST 请求。但是 DELETE 请求会抛出上述错误。

I've seen solutions on here that involve the pList file, but none of them have worked for me. I've listed my attempts below.

我在这里看到了涉及 pList 文件的解决方案,但没有一个对我有用。我在下面列出了我的尝试。

First attempt:

第一次尝试:

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

Second try:

第二次尝试:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>herokuapp.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
            <key>NSExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <false/>
            <key>NSThirdPartyExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSRequiresCertificateTransparency</key>
            <false/>
        </dict>
    </dict>
</dict>

And finally, I even put all these temporary keys in like so:

最后,我什至把所有这些临时密钥都放进去了:

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>herokuapp.com</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSTemporaryIncludesSubdomains</key>
                <true/>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSExceptionRequiresForwardSecrecy</key>
                <false/>
                <key>NSTemporaryExceptionRequiresForwardSecrecy</key>
                <false/>
                <key>NSExceptionMinimumTLSVersion</key>
                <string>TLSv1.2</string>
                <key>NSTemporaryExceptionMinimumTLSVersion</key>
                <string>TLSv1.2</string>
                <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSTemporaryThirdPartyExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
                <false/>
                <key>NSTemporaryThirdPartyExceptionRequiresForwardSecrecy</key>
                <false/>
                <key>NSThirdPartyExceptionMinimumTLSVersion</key>
                <string>TLSv1.2</string>
                <key>NSTemporaryThirdPartyExceptionMinimumTLSVersion</key>
                <string>TLSv1.2</string>
                <key>NSRequiresCertificateTransparency</key>
                <false/>
                <key>NSTemporaryRequiresCertificateTransparency</key>
                <false/>
            </dict>
        </dict>
    </dict>

All with no luck! I always get the same error. The DELETE request is formatted correctly because when I manually do it from Postman, I get the desired result.

一切都没有运气!我总是得到同样的错误。DELETE 请求的格式正确,因为当我从 Postman 手动执行它时,我得到了想要的结果。

Here is what my actual API call method looks like, just in case there could be an issue here:

这是我的实际 API 调用方法的样子,以防万一这里可能出现问题:

class func makeDELETEALLRequest(completion: (error:Bool) -> Void) {
        let session = NSURLSession.sharedSession()
        let url = NSURL(string:"https://mysubdomain.herokuapp.com/42kh24kh2kj2g24/clean")
        let request = NSMutableURLRequest(URL: url!)
        request.HTTPMethod = "DELETE"

        let task = session.dataTaskWithRequest(request) { (data, response, error) -> Void in

            if (error != nil) {
                print("Error making API call: \(error!)")
                completion(error: true)
            } else {
                let HTTPResponse = response as! NSHTTPURLResponse
                let statusCode = HTTPResponse.statusCode
                if (statusCode == 200){
                    print("Successfully deleted!")
                    completion(error: false)
                } else {
                    print("Different status code: \(statusCode)")
                    completion(error: true)
                }
            }
        }
        task.resume()
    }

Once again, I'm using Xcode 7 beta 6.

再一次,我使用的是Xcode 7 beta 6

ABOUT MY SELECTED ANSWERThe answer I selected as correct was right for me because I made all these changes to the wrong pList file in my project and that answer was the only one that addressed the possibility. The solutions offered by the other answers are not wrong, so any other people experiencing this issue should give them a try, since they are valid. I hope this helps anyone having similar issues.

关于我选择的答案 我选为正确的答案对我来说是正确的,因为我对项目中错误的 pList 文件进行了所有这些更改,而该答案是唯一解决了这种可能性的答案。其他答案提供的解决方案没有错,因此遇到此问题的任何其他人都应该尝试一下,因为它们是有效的。我希望这可以帮助任何有类似问题的人。

采纳答案by Jase68

I, too, had trouble overriding App Transport Security after upgrading to xCode 7.0, and tried the same kinds of solutions you have to no avail. After walking away from it for awhile, I noticed that I had made the changes to the Info.plist under Supporting Files of "MyAppName Tests" rather than the one in the project itself. The Supporting Files folder in my project wasn't expanded, so I hadn't even noticed the Info.plist in it.

升级到 xCode 7.0 后,我也无法覆盖 App Transport Security,并尝试了相同类型的解决方案,但无济于事。离开它一段时间后,我注意到我对“MyAppName 测试”的支持文件下的 Info.plist 进行了更改,而不是项目本身中的更改。我的项目中的 Supporting Files 文件夹没有展开,所以我什至没有注意到其中的 Info.plist。

Typical amateur mistake, I'm sure, but they're only a couple of lines apart in the Project Navigator and it had me frustrated until I noticed the distinction. Thought I'd mention it in case you're having the same problem.

我敢肯定,这是典型的业余错误,但它们在 Project Navigator 中仅相隔几行,这让我很沮丧,直到我注意到了区别。我想我会提到它以防万一你遇到同样的问题。

回答by Manab Kumar Mal

I have solved it with adding some key in info.plist. As I am using objective C for some native application.

我已经通过在 info.plist 中添加一些键来解决它。因为我将目标 C 用于某些本机应用程序。

The steps I followed are:

我遵循的步骤是:

  1. Opened my Projects info.plistfile

  2. Added a Key called NSAppTransportSecurityas a Dictionary.

  3. Added a Subkey called NSAllowsArbitraryLoadsas Booleanand set its value to YESas like following image.
  1. 打开我的项目info.plist文件

  2. 增加了一个名为关键NSAppTransportSecurityDictionary

  3. 添加了一个名为NSAllowsArbitraryLoadsas的子项Boolean并将其值设置YES为如下图所示。

enter image description here

enter image description here

Clean the Project and Now Everything is Running fine as like before.

清理项目,现在一切都像以前一样正常运行。

Ref Link:

参考链接:

  1. https://stackoverflow.com/a/32631185/2905967

  2. https://stackoverflow.com/a/32609970

  1. https://stackoverflow.com/a/32631185/2905967

  2. https://stackoverflow.com/a/32609970

回答by Carl Hine

Appreciate you've tried adding the following, to your plist file:

感谢您尝试将以下内容添加到您的 plist 文件中:

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

... you might want to try to change your line:

...您可能想尝试更改您的线路:

let url = NSURL(string:"https://mysubdomain.herokuapp.com/42kh24kh2kj2g24/clean")

to:

到:

let url = NSURL(string:"http://mysubdomain.herokuapp.com/42kh24kh2kj2g24/clean")

Apologies if you have tried this. I can understand how frustrating it is when you think you've exhausted all avenues.

抱歉,如果您尝试过此操作。我可以理解当您认为自己已经用尽所有途径时是多么令人沮丧。

But as soon as I ran up my App on Xcode 7, so that I could test our Apps, one kicked off with the "App Transport Security" problem. We're using Oracle-based web-services and it's too late in the day to start configuring digital certificates for SSL-based HTTP. So, the above addition to my plist file did the trick. Appreciate you say you've tried this. But, just to help anyone else, it did actually work for me. It need to as I have no immediate way of enabling SSL on our Oracle box.

但是,当我在 Xcode 7 上运行我的应用程序以便我可以测试我们的应用程序时,“应用程序传输安全”问题就开始了。我们正在使用基于 Oracle 的 Web 服务,现在开始为基于 SSL 的 HTTP 配置数字证书为时已晚。因此,上述添加到我的 plist 文件中就成功了。感谢您说您已经尝试过了。但是,只是为了帮助其他人,它确实对我有用。它需要,因为我没有立即在我们的 Oracle 机器上启用 SSL 的方法。

回答by Hamed

In Xcode 8 & Xcode 9

在 Xcode 8 和 Xcode 9 中

  1. Open my Projects info.plist file
  2. Add a Key called AppTransportSecurityas a Dictionary.
  3. Add a Subkey called AllowsArbitraryLoadsas Boolean and set its value to YES as like following image.
  1. 打开我的项目 info.plist 文件
  2. 添加一个称为AppTransportSecurity字典的键。
  3. 添加一个名为AllowsArbitraryLoadsBoolean的子键并将其值设置为 YES,如下图所示。

enter image description here

enter image description here

回答by Juseman Andrey

I have solved as plist file.
1. Add a NSAppTransportSecurity : Dictionary.
2. Add Subkey named NSAllowsArbitraryLoadsas Boolean : YES

我已经解决了 plist 文件。
1. 添加一个NSAppTransportSecurity : Dictionary.
2.添加子项命名NSAllowsArbitraryLoadsBoolean : YES

This worked well.

这工作得很好。